Advertisements
I have 2 lists
list1 = ["ben", "tim", "john", "wally"]
list2 = [18,12,34,55]
the output im looking for is this
[{'Name': 'ben', 'Age': 18, 'Name': 'tim', 'Age': 12, 'Name': 'john', 'Age': 34, 'Name': 'wally', 'Age': 55}]
>Solution :
use a list comprehension to combine the two lists into a list of dictionaries like this:
list1 = ["ben", "tim", "john", "wally"]
list2 = [18,12,34,55]
result = [{'Name': name, 'Age': age} for name, age in zip(list1, list2)]
result
will output this:
[{'Name': 'ben', 'Age': 18},
{'Name': 'tim', 'Age': 12},
{'Name': 'john', 'Age': 34},
{'Name': 'wally', 'Age': 55}]