简体   繁体   中英

Combining 3 lists to create a list of dictionaries

I'm trying to figure out the best way to take the following 3 lists:

name_list = ["potatoes", "milk", "butter"]
quant_list = [500, 200, 40]
unit_list = ["g", "ml", "g"]

and convert them to a list of dictionaries, in the following format:

target_list = [{"name": "potatoes", "quant_units": {"amount": 500, "unit": "g"}}, {"name": "milk", "quant_units": {"amount": 200, "unit": "ml"}}, {"name": "butter", "quant_units": {"amount": 40, "unit": "g"}}]

I'm really struggling to figure out the methods, loops required to do this.

Any pointers would be much appreciated!

You can use zip :

res = []
for (a, b, c) in  zip(name_list, quant_list, unit_list):
    res.append({'name': a, 'quant_units': {'amount':b, 'unit':c}})
print(res)

Output:

[{'name': 'potatoes', 'quant_units': {'amount': 500, 'unit': 'g'}}, {'name': 'milk', 'quant_units': {'amount': 200, 'unit': 'ml'}}, {'name': 'butter', 'quant_units': {'amount': 40, 'unit': 'g'}}]

Try this:

name_list = ["potatoes", "milk", "butter"]
quant_list = [500, 200, 40]
unit_list = ["g", "ml", "g"]
ans = []
for i,j,k in zip(name_list, quant_list, quant_list):
    ans.append({'name':i,'quant':j,'unit':k})
print(ans)

Output:

[{'name': 'potatoes', 'quant': 500, 'unit': 500}, {'name': 'milk', 'quant': 200, 'unit': 200}, {'name': 'butter', 'quant': 40, 'unit': 40}]

You could also use list comprehension, which would make it a one-liner and generally is a really cool concept in python

name_list = ["potatoes", "milk", "butter"]
quant_list = [500, 200, 40]
unit_list = ["g", "ml", "g"]

[{'name': f'{food}', 'quant_units': {'amount': amount, 'unit': unit}}
for food, amount, unit in zip(name_list, quant_list, unit_list)]

Output:

[{'name': 'potatoes', 'quant_units': {'amount': 500, 'unit': 'g'}},
 {'name': 'milk', 'quant_units': {'amount': 200, 'unit': 'ml'}},
 {'name': 'butter', 'quant_units': {'amount': 40, 'unit': 'g'}}]

You can use this:

name_list = ["potatoes", "milk", "butter"]
quant_list = [500, 200, 40]
unit_list = ["g", "ml", "g"]

lst = []

for n, q, u in zip(name_list, quant_list, unit_list):
    lst.append({'name': n, 'quant_units': {'amount': q, 'unit': u}})

print(lst)

Output:

[{'name': 'potatoes', 'quant_units': {'amount': 500, 'unit': 'g'}}, {'name': 'milk', 'quant_units': {'amount': 200, 'unit': 'ml'}}, {'name': 'butter', 'quant_units': {'amount': 40, 'unit': 'g'}}]

You can also use this as an alternative solution if you have an equal number of list items:

list_dict = []
for i in range(len(name_list)):
    list_dict.append({'name':name_list[i], 'qaunt_list':{'amount':quant_list[i], 'unit':unit_list[i]}})

output:

[{'name': 'potatoes', 'qaunt_list': {'amount': 500, 'unit': 'g'}},
 {'name': 'milk', 'qaunt_list': {'amount': 200, 'unit': 'ml'}},
 {'name': 'butter', 'qaunt_list': {'amount': 40, 'unit': 'g'}}]
       

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