从python中的字典列表中获取特定索引项的列表(列表理解)

Hec*_*out 1 python

我有一个像这样的词典列表:

listDict = [{'id':1,'other':2},{'id':3,'other':4},{'id':5,'other':6}]
Run Code Online (Sandbox Code Playgroud)

我想要一个列表中所有id的列表.所以,从给定的列表中我会得到列表:

[1,3,5]
Run Code Online (Sandbox Code Playgroud)

它应该是一行.我知道我以前做过这个,我只是忘记了语法......谢谢

bal*_*pha 11

>>> listDict = [{'id':1,'other':2},{'id':3,'other':4},{'id':5,'other':6}]
>>> [item["id"] for item in listDict]
[1, 3, 5]
Run Code Online (Sandbox Code Playgroud)