Skip to content
Advertisement

Fastest method: for value in DictA, find value in DictB and retrieve other DictB value?

For each item in dictA, I want to search for it in dictB, if dictB has it then I want to pull some other values from dictB and add it to dictA.

An example that is working is here, however it is rather slow as I have 50,000+ items to search through and it will perform this similar function on multiple dicts.

Is there a fast method of performing this search?

dictA = [
    {'id': 12345},
    {'id': 67890},
    {'id': 11111},
    {'id': 22222}
]

dictB = [
    {'id': 63351, 'name': 'Bob'},
    {'id': 12345, 'name': 'Carl'},
    {'id': 59933, 'name': 'Amy'},
    {'id': 11111, 'name': 'Chris'}
]

for i in dictA:
    name = None
    for j in dictB:
        if i['id'] == j['id']:
            name = j['name']
    i['name'] = name

The dictA output after this would be:

dictA = [
    {'id': 12345, 'name': 'Carl'},
    {'id': 67890, 'name': None},
    {'id': 11111, 'name': 'Chris'},
    {'id': 22222, 'name': None}
]

Advertisement

Answer

  • The given is list of dict. You can create dict from that assuming id is uninque. Converting from list of dict to dict will work for your case.
dictA = [
    {'id': 12345},
    {'id': 67890},
    {'id': 11111},
    {'id': 22222}
]

dictB = [
    {'id': 63351, 'name': 'Bob'},
    {'id': 12345, 'name': 'Carl'},
    {'id': 59933, 'name': 'Amy'},
    {'id': 11111, 'name': 'Chris'}
]


actual_dictB = dict()
for d in dictB:
    actual_dictB[d['id']] = d['name']

for i in dictA:
    i['name'] = actual_dictB.pop(i['id'], None) # now search have became O(1) constant. So best time complexity achived O(n) n=length of dictA

print(dictA)
  • Follow up for additional question:
actual_dictB = dict()
for d in dictB:
    id_ = d['id']
    d.pop('id')
    actual_dictB[id_] = d

    
tmp = dict([(k,None) for k in dictB[0].keys() if k!='id'])
           
for i in dictA:
    if i['id'] not in actual_dictB:
        i.update(tmp)
    else:
        i.update(actual_dictB[i['id']])
print(dictA)
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement