Home > Mobile >  Given the value of a dictionary field, how can I find a dictionary in a list of dictionaries?
Given the value of a dictionary field, how can I find a dictionary in a list of dictionaries?

Time:09-21

Based on a list of fastfoods (list of dictionaries), for each fastfood (each dictionary), I'm trying to 1) extract the competitor's name and then 2) use that competitor's name to retrieve its shortname.

Currently my solution really doesn't make sense and I have a feeling it might be recursive? I'm really having an issue conceptualizing this.

fastfoods = [{'id': 30, 'name': 'McDonalds', 'shortname': 'MC', 'competitor': 'BurgerKing'}, {'id': 47, 'name': 'BurgerKing', 'shortname': 'BK', 'competitor': None}]

for fastfood in fastfoods:
    competitor_name = fastfood.get('competitor')
    short_name = fastfood.get('shortname')

    for fastfood in fastfoods:
        if competitor_name == short_name:
            print(fastfood.get('shortname')

Here's a visualization of what I'm trying to achieve: enter image description here

In this limited example I have (real example has thousands of dictionaries, but I'm using 2 just for the example.

So here, I loop over the dictionaries, I reach the first dictionary, I extract the competitor's name ('BurgerKing'). At this point, I want to search for 'BurgerKing' as a 'name' field (not as a competitor field). Once that's found, I access that dictionary where the 'name' field == 'BurgerKing' and extract the shortname ('BK').

CodePudding user response:

I think you're looking for something like this:

byName = {dct['name']:dct for dct in fastfoods}
for fastfood in fastfoods:
    if 'competitor' in fastfood and fastfood['competitor'] is not None:
        competitor = byName[fastfood['competitor']]
        if 'shortname' in competitor:
            print(competitor['shortname'])
        else:
            print(f"competitor {fastfood['competitor']} has no shortname")

Explanation:

  • create byName, a dictionary that indexes dicts in fastfoods by their name entry
  • iterate over all dicts in fastfoods
  • if a given dict has a competitor entry and it's non-null, look up the dict for that competitor by name in byName and if it has a shortname entry print it
  • otherwise print a message indicating there is no shortname entry for the competitor (you can do something different in this case if you like).

CodePudding user response:

I would first construct a dictionary that maps a name to its shortened version, and then use it. This would be way faster than looking for the competitor in the list over and over again.

fastfoods = [{'id': 30, 'name': 'McDonalds', 'shortname': 'MC', 'competitor': 'BurgerKing'}, {'id': 47, 'name': 'BurgerKing', 'shortname': 'BK', 'competitor': None}]

name_to_short = {dct['name']: dct['shortname'] for dct in fastfoods}

for dct in fastfoods:
    print(f"The competitor of {dct['name']} is: {name_to_short.get(dct['competitor'], 'None')}")

# The competitor of McDonalds is: BK
# The competitor of BurgerKing is: None
  • Related