首页 > 解决方案 > 在python问题中合并两个列表

问题描述

我在 python 中有两个列表

名称:

['Test 1', 'Test 2']

是真的:

[False, True]

我想将列表合并到一个列表中,结果是:

[{name: 'Test 1', isTrue: False}, {name: 'Test 2', isTrue: True}]

这是我尝试过的:

list = []
thisdict = {}
for name in names:
    thisdict['name'] = name
    thisdict['isTrue'] = True
    list.append(thisdict)

问题是我不确定如何获得动态布尔值或如何更有效地做到这一点。

任何帮助将不胜感激。

标签: pythonpython-3.xlistdictionary

解决方案


您可以使用列表理解和zip

>>> [dict(name=x, isTrue=y) for x, y in zip(names, isTrue)]

推荐阅读