Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python dictionary help
#1
Hello,

I accessed an API and I got this below.

How can I extract the email_address and FNAME from this dictionary?

>>> type(b)
<class 'dict'>
>>> print(b)
{'members': [{'id': 'f149ad5b63bba504497687010d4809bd', 'email_address': '[email protected]', 'merge_fields': {'FNAME': 'Pepe', 'LNAME': 'Lopez', 'ADDRESS': {'addr1': '', 'addr2': '', 'city': '', 'state': '', 'zip': '', 'country': 'US'}, 'PHONE': '', 'BIRTHDAY': ''}}, {'id': '58ccf84f5702a7be79e06fd6ed933d37', 'email_address': '[email protected]', 'merge_fields': {'FNAME': 'Marc', 'LNAME': 'Perez', 'ADDRESS': '', 'PHONE': '', 'BIRTHDAY': ''}}]}
>>> print(b.keys())
dict_keys(['members'])
>>> print(b.values())
dict_values([[{'id': 'f149ad5b63bba504497687010d4809bd', 'email_address': '[email protected]', 'merge_fields': {'FNAME': 'Pepe', 'LNAME': 'Lopez', 'ADDRESS': {'addr1': '', 'addr2': '', 'city': '', 'state': '', 'zip': '', 'country': 'US'}, 'PHONE': '', 'BIRTHDAY': ''}}, {'id': '58ccf84f5702a7be79e06fd6ed933d37', 'email_address': '[email protected]', 'merge_fields': {'FNAME': 'Marc', 'LNAME': 'Perez', 'ADDRESS': '', 'PHONE': '', 'BIRTHDAY': ''}}]])
Thanks
Reply
#2
for member in b['members']:
    email = member.get('email_address')
    fname = member['merge_fields'].get('FNAME')
and b is poor name, use something meaningful instead
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
for member in b['members']:
    print(member['email-address'])
    print(member['merge-fields']['FNAME'])
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#4
Thanks, it worked!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Convert List of Dictionary to dictionary of dictionary list in python kk230689 2 49,842 Apr-27-2019, 03:13 AM
Last Post: perfringo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020