Hector Scout Hector Scout - 1 year ago 60
Python Question

Getting a list of specific index items from a list of dictionaries in python (list comprehension)

I have a list of dictionaries like so:

listDict = [{'id':1,'other':2},{'id':3,'other':4},{'id':5,'other':6}]

I want a list of all the ids from the dictionaries. So, from the given list I would get the list:


It should be one line. I know I've done this before, I've just forgotten the syntax...Thanks

Answer Source
>>> listDict = [{'id':1,'other':2},{'id':3,'other':4},{'id':5,'other':6}]
>>> [item["id"] for item in listDict]
[1, 3, 5]
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download