Alo
Alo

Reputation: 67

Convert list of dictionaries with different keys to values string list

Example list of dicts:

[{'name': 'aly', 'age': '104'}, {'name': 'Not A name', 'age': '99'}]

Expected out = ['aly', '104', 'Not A name', '99']

Any help will be much appreciated.

Thanks!

Upvotes: 2

Views: 66

Answers (2)

Andrew
Andrew

Reputation: 166

Another way :

listDictionary = [{'name': 'aly', 'age': '104'}, {'name': 'Not A name', 'age': '99'}]
out = []
for i in listDictionary:
    for k, v in i.items():
        out.append(v)
print(out)

Output : ['aly', '104', 'Not A name', '99']

Upvotes: 1

Mehrdad Pedramfar
Mehrdad Pedramfar

Reputation: 11083

Try this in one line:

d = [{'name': 'aly', 'age': '104'}, {'name': 'Not A name', 'age': '99'}]

[v for i in d for k,v in i.items()]

The result will be:

Out[1]: ['aly', '104', 'Not A name', '99']

Upvotes: 1

Related Questions