简体   繁体   中英

How can I iterate over a list of dictionaries and create new dictionary

I am trying to iterate over a list of dictionaries below, and where the 'name' == 'name' of another dict, the prices (in this case its the value of 'Variant OG' or 'Bad Axe') are put into a single dict.

[{'menu': '', 'category': 'Production Item', 'group': '', 'item_id': 1354, 'item': 'Giardiniera', 'Variant OG': 5.0},
 {'menu': '', 'category': 'Production Item', 'group': '', 'item_id': 1355, 'item': 'Sweet Peppers', 'Variant OG': 5.0},
 {'menu': '', 'category': 'Production Item', 'group': '', 'item_id': 1334, 'item': 'Hot Bar Serving Hardware', 'Variant OG': 5.0},
 {'menu': '', 'category': 'Production Item', 'group': '', 'item_id': 1354, 'item': 'Giardiniera', 'Bad Axe': 1.0},
 {'menu': '', 'category': 'Production Item', 'group': '', 'item_id': 1355, 'item': 'Sweet Peppers', 'Bad Axe': 1.0},
 {'menu': '', 'category': 'Production Item', 'group': '', 'item_id': 1334, 'item': 'Hot Bar Serving Hardware', 'Bad Axe': 1.0}]

Basically, all same menu items are in the same dict, but also have their different prices included

Ideally, the end result would look like

{'menu': '', 'category': 'Production Item', 'group': '', 'item_id': 1354, 'item': 'Giardiniera', 'Variant OG': 5.0, 'Bad Axe': 1.0}
{'menu': '', 'category': 'Production Item', 'group': '', 'item_id': 1355, 'item': 'Sweet Peppers', 'Variant OG': 5.0, 'Bad Axe': 1.0}
{'menu': '', 'category': 'Production Item', 'group': '', 'item_id': 1334, 'item': 'Hot Bar Serving Hardware', 'Variant OG': 5.0, 'Bad Axe': 1.0}

It could be easier if you first divide the list_of_dictionaries into 2 different lists so that the other one contains all the 'Variant OG's and the other one all the 'Bad Axe's.

Then you can easily iterate through them eg

for variant in variant_OGs:
       for bad in bad_axes:
             if( variant['item']==bad['item']):
                   #put them into single dict

It looks like each list only varies by Variant OG or Bad Axe , so sorting by the item or item_id and using itertools.groupby to iterate over items with matching keys will solve the problem:

from itertools import groupby

lst = [{'menu':'','category':'Production Item','group':'','item_id':1354,'item':'Giardiniera','Variant OG':5.0},
       {'menu':'','category':'Production Item','group':'','item_id':1355,'item':'Sweet Peppers','Variant OG':5.0},
       {'menu':'','category':'Production Item','group':'','item_id':1334,'item':'Hot Bar Serving Hardware','Variant OG':5.0},
       {'menu':'','category':'Production Item','group':'','item_id':1354,'item':'Giardiniera','Bad Axe':1.0},
       {'menu':'','category':'Production Item','group':'','item_id':1355,'item':'Sweet Peppers','Bad Axe':1.0},
       {'menu':'','category':'Production Item','group':'','item_id':1334,'item':'Hot Bar Serving Hardware','Bad Axe':1.0}]


# groupby requires the container to be sorted by the key
sortkey = lambda x: x['item_id']
lst.sort(key=sortkey)

# "key" is the common key for the items
# "items" is an iterator of the common items
for key,items in groupby(lst,key=sortkey):
    # start with an empty dict for each common key
    dct = {}
    for item in items:
        # add all key/value pairs for each common key item to the dict
        dct.update(item)
    print(dct)

Output:

{'menu': '', 'category': 'Production Item', 'group': '', 'item_id': 1334, 'item': 'Hot Bar Serving Hardware', 'Variant OG': 5.0, 'Bad Axe': 1.0}
{'menu': '', 'category': 'Production Item', 'group': '', 'item_id': 1354, 'item': 'Giardiniera', 'Variant OG': 5.0, 'Bad Axe': 1.0}
{'menu': '', 'category': 'Production Item', 'group': '', 'item_id': 1355, 'item': 'Sweet Peppers', 'Variant OG': 5.0, 'Bad Axe': 1.0}

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