删除列表中的项目并获取新列表?

时间:2016-11-21 09:59:43

标签: python list

我看到有几个关于从列表中删除项目的主题,包括使用remove()pop()del。但这些都不是我想要的,因为我想在删除项目时获得一个新列表。例如,我想这样做:

a = [1, 2, 3, 4, 5, 6]
<fill in >       # This step somehow removes the third item and get a new list b and let
b = [1, 2, 4, 5, 6]

我该怎么做?

1 个答案:

答案 0 :(得分:7)

如果你想要一个没有第三个元素的新列表,那么:

b = a[:2] + a[3:]

如果您想要一个没有价值的列表&#39; 3&#39;:

b = [n for n in a if n != 3]
相关问题