Home > Mobile >  How do I combine a list of 1 item dicts into a list
How do I combine a list of 1 item dicts into a list

Time:11-19

As a result of a pymongo query I have a list of 1 item dicts:

[{'id': 453},
 {'id': 365},
 {'id': 311},
 {'id': 523},
 {'id': 367},
 {'id': 891},
 {'id': 621},
 {'id': 215},
 {'id': 819},
 {'id': 218},
 {'id': 711},
 {'id': 212},
 {'id': 409},
 {'id': 200},
 {'id': 893},
 {'id': 219},
 {'id': 832},
...]

I would like to simply have a list of these values : [453,365,...] is there a function that accomplishes this? Otherwise, I guess I could loop through it:

my_list = []
[my_list.append(i['id']) for i in ids]

CodePudding user response:

You can make it a oneliner:

my_list = [i['id'] for i in ids]
  • Related