Multiple Dictionaries into List

Viewed 54
data = [
    {"a": 1, "b": 2, "c": 3},
    {"a": 4, "c": 6, "b": 5}
]

thats my data, I can't get this to work:

for i in data:
       c= [(k,v) for k,v in data[i].items()]
       c.sort()
       print(c)

I need to iterate through a list of dictionaries. After i got the List I need to get the "v's" into a new List.

Thanks in advance!

[('a', 1), ('b', 2), ('c', 3)]

thats how the lists should look like

1 Answers

The i in for i in data is the dict itself so use:

data = [
    {"a": 1, "b": 2, "c": 3},
    {"a": 4, "c": 6, "b": 5}
]

for i in data:
       c= [(k,v) for k, v in i.items()]
       c.sort()
       print(c)
Related