简体   繁体   中英

Extracting matching values from nested dictionary

I'm trying to extract values from a nested dictionary if a value matches the value in a list.

data = [
            {
                "id": 12345678,
                "list_id": 12345,
                "creator_id": 1234567,
                "entity_id": 1234567,
                "created_at": "2020-01-30T00:43:55.256-08:00",
                "entity": {
                    "id": 123456,
                    "type": 0,
                    "first_name": "John",
                    "last_name": "Doe",
                    "primary_email": "john@fakemail.com",
                    "emails": [
                        "john@fakemail.com"
                    ]
                }
            },
            {
                "id": 12345678,
                "list_id": 12345,
                "creator_id": 1234567,
                "entity_id": 1234567,
                "created_at": "2020-01-30T00:41:54.375-08:00",
                "entity": {
                    "id": 123456,
                    "type": 0,
                    "first_name": "Jane",
                    "last_name": "Doe",
                    "primary_email": "jane@fakemail.com",
                    "emails": [
                        "jane@fakemail.com"
                    ]
                }
            }
        ]

The code is as follows.

match_list = ['jane@fakemail.com',[]]
first_names = []
email = []
for i in match_list:
    for record in data:
        if 'primary_email' == i:
            email.append(record.get('entity',{}).get('primary_email', None))
            first_names.append(record.get('entity',{}).get('first_name', None))       
print(first_names)
print(email)

Instead of returning the matching values this only returns empty lists. Any help here would be much appreciated.

The expected output is

first_names = ['Jane'] and email = ['jane@fakemail.com']

Store temporary values in variables, to make your code easier to handle:

emails = []
names = []
match_list = ['jane@fakemail.com',[]]


for item in data:
    entry = item.get('entity', {})

    fName = entry.get('first_name', '')
    pMail = entry.get('primary_email', '')

    if pMail in match_list:
        print (fName)
        print (pMail)

        emails.append(pMail)
        names.append(fName)

Output:

Jane
jane@fakemail.com

In the 6th line of your code

    if 'primary_email' == i:

You're comparing elements from match_list (that is 'i') to literally the string called 'primary_email' instead of the actual email. since 'jane@fakemail.com' is not equal to 'primary_email' (literally the string).

Instead use

if record['entity']['primary_email'] == i:

and your code should work as expected.

In your code you would always get an empty list as you are comparing 'primary_email'==i which will always be False .

Change it to record['entity']['primary_email']==i .

And here there is no need to use get .Since if mail doesn't match with any of the primary_email then nothing happens. primary_email will only be added when it meets the condition d['entity']['primary_email']==mail .

Try this I refactored your code little bit.

In [25]: for mail in match_list:
    ...:     for d in data:
    ...:         if d['entity']['primary_email']==mail:
    ...:             first_name.append(d['entity']['first_name'])
    ...:             emails.append(d['entity']['primary_email'])

output

In [26]: emails
Out[26]: ['jane@fakemail.com']

In [27]: first_name
Out[27]: ['Jane']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM