Saeed
Saeed

Reputation: 4123

How to check the occurrences of a dictionary key in another list and create new output

I have a list and a dictionary like this:

mydict = {
    'some-id-string1': {'name': 'Saeed1', 'phone': '+989307333730', 'id': 'abc'},
    'some-id-string2': {'name': 'Saeed2', 'phone': '+989307333731', 'id': 'def'},
    'some-id-string3': {'name': 'Saeed3', 'phone': '+989307333732', 'id': 'ghi'},
    'some-id-string4': {'name': 'Saeed3', 'phone': '+989307333733', 'id': 'jkl'},
    'some-id-string5': {'name': 'Saeed3', 'phone': '+989307333730', 'id': 'abc'},
    'some-id-string6': {'name': 'Saeed3', 'phone': '+989307333730', 'id': 'abc'},
    'some-id-string7': {'name': 'Saeed3', 'phone': '+989307333731', 'id': 'def'},

}
mylist = [
    {'id': 'abc', 'name': 'some_name1'},
    {'id': 'def', 'name': 'some_name2'},
    {'id': 'ghi', 'name': 'some_name3'},
]

I'm going to check if the id value of the mydict matches mylist and add the name of mylist to mydict. If that doesn't exist there, I'm going to have two output (because I'm not sure currently to keep which one, so I'm going to have both to decide later):

Expected output 1 if name exists and to add NOT_FOUND for the ones not having corresponding name:

newmydict1 = {
    'some-id-string1': {'name': 'Saeed1', 'phone': '+989307333730', 'id': 'abc', 'id_name': 'some_name1'},
    'some-id-string2': {'name': 'Saeed2', 'phone': '+989307333731', 'id': 'def', 'id_name': 'some_name2'},
    'some-id-string3': {'name': 'Saeed3', 'phone': '+989307333732', 'id': 'ghi', 'id_name': 'some_name3'},
    'some-id-string4': {'name': 'Saeed3', 'phone': '+989307333733', 'id': 'jkl', 'id_name': 'NOT_FOUND'},
    'some-id-string5': {'name': 'Saeed3', 'phone': '+989307333730', 'id': 'abc', 'id_name': 'some_name1'},
    'some-id-string6': {'name': 'Saeed3', 'phone': '+989307333730', 'id': 'abc', 'id_name': 'some_name1'},
    'some-id-string7': {'name': 'Saeed3', 'phone': '+989307333731', 'id': 'def', 'id_name': 'some_name2'},
}

Expected output 2 if name exists and to create a new list for the ones not having corresponding name:

newmydict2 {
    'some-id-string1': {'name': 'Saeed1', 'phone': '+989307333730', 'id': 'abc', 'id_name': 'some_name1'},
    'some-id-string2': {'name': 'Saeed2', 'phone': '+989307333731', 'id': 'def', 'id_name': 'some_name2'},
    'some-id-string3': {'name': 'Saeed3', 'phone': '+989307333732', 'id': 'ghi', 'id_name': 'some_name3'},
    'some-id-string5': {'name': 'Saeed3', 'phone': '+989307333730', 'id': 'abc', 'id_name': 'some_name1'},
    'some-id-string6': {'name': 'Saeed3', 'phone': '+989307333730', 'id': 'abc', 'id_name': 'some_name1'},
    'some-id-string7': {'name': 'Saeed3', 'phone': '+989307333731', 'id': 'def', 'id_name': 'some_name2'},
}
newmydict3 = {
    'some-id-string4': {'name': 'Saeed3', 'phone': '+989307333733', 'id': 'jkl'},
}

I'm not sure what to do and what to write, because the key of mydict (I mean some-id-string) does not exist in mylist, and also some members of the mydict['id'] has more than one occurences.

I tried

new = [{
    'string_id': d, 
    'id': l['id'], 
    'id_name': l['name'], 
    'phone': mydict[d]['phone'], 
    'name': mydict[d]['name']
    } for l in mylist for d in mydict if mydict[d]['id'] == l['id']
]

Upvotes: 0

Views: 71

Answers (1)

Barmar
Barmar

Reputation: 782166

You're pretty close, but new is a list, you want a dictionary. So use a dictionary comprehension rather than a list comprehension.

newmydict2 = {key: {
    'id': l['id'], 
    'id_name': l['name'], 
    'phone': value['phone'], 
    'name': value['name']
    } for l in mylist for key, value in mydict.items() if value['id'] == l['id']
}

newmydict3 = {key: value for key, value in mydict.items() if key not in newmydict2}

Also, use mydict.items() so you can access the key and value with variables rather than repeating mydict[d].

If mylist is large, it would be more efficient to convert it to a dictionary first, rather than searching it repeatedly.

mylist_dict = {l['id']: l['name'] for l in mylist}

newmydict2 = {key: {
    'id': value['id'], 
    'id_name': mylist_dict[value['id']], 
    'phone': value['phone'], 
    'name': value['name']
    } for key, value in mydict.items() if value['id'] in mylist_dict
}

You could also convert your input to pandas dataframes, then use its merge and difference operations.

Upvotes: 1

Related Questions