首页 > 解决方案 > 两个列表中的值

问题描述

所以我有两个清单。例如:

list1 = [a1, b2, c3, f6]
list2 = [a1, b2, d4, e5]

而且我只希望在 list2 中唯一出现的值附加到新列表中。例如:

list3 = [d4, e5]

标签: pythonlistif-statement

解决方案


你可以使用一个list comprehension.

list3 = [item for item in list2 if item not in list1]

输出

list3 = ['d4', 'e5']

推荐阅读