tags:

views:

139

answers:

4

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:

[1,3,5]

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

+2  A: 
[i['id'] for i in listDict]
SilentGhost
A: 

[elem['id'] for elem in listDict]

+6  A: 
>>> listDict = [{'id':1,'other':2},{'id':3,'other':4},{'id':5,'other':6}]
>>> [item["id"] for item in listDict]
[1, 3, 5]
balpha
Exactly what I was looking for. Thanks.
Hector Scout
And thanks back for my first rep. :-) /me happy
balpha
A: 

For the python geeks:

import operator
map(operator.itemgetter('id'), listDict)
ʞɔıu