Home > other >  Combining 3 lists to create a list of dictionaries
Combining 3 lists to create a list of dictionaries

Time:07-11

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!

CodePudding user response:

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'}}]

CodePudding user response:

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'}}]

CodePudding user response:

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'}}]
       

CodePudding user response:

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}]

CodePudding user response:

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'}}]
  • Related