'List of multiple dictionaries
Very simple question that I can’t find an answer for.
Let’s say i have a list of multiple lists inside this list.
List_1 = [[“name”,”Richard”,”age”,36],[“name”,”Andrea”,”age”,44],[”name”,”Steve”,”age”,56]]
What i would need is to convert each list inside “main” list to dictionary so as a result there would be 1 list where inside is multiple dictionaries.
Thanks for helping me out this is just making me crazy..
Solution 1:[1]
All you need is a basic for loop.
new_list = []
for sublist in list_1:
Put your code to convert the sublist to a dict here
new_list.append(dictionary)
Solution 2:[2]
This should work:
for i in range(len(List_1)):
List_1[i] = {'name': List_1[i][1], 'age': List_1[i][3]}
Where List_1 is a list of the type defined in the question.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Nick Bailey |
| Solution 2 | catasaurus |
