使用dict()函数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
list_ = [
{
"id": "11",
"name": "12",
"other": "13"
},
{
"id": "21",
"name": "22",
"other": "23"
},
{
"id": "31",
"name": "32",
"other": "33"
},
]
test = dict([(i["id"], i["name"]) for i in list_])
print(test)
1
{'11': '12', '21': '22', '31': '32'}

还可以使用生成器表达式的方式

1
2
test = {i["id"]: i["name"] for i in list_}
print(test)
1
{'11': '12', '21': '22', '31': '32'}