从 Python 中的列表列表中删除空列表
Remove the empty lists from a list of lists in Python
使用列表理解从列表列表中删除空列表,例如
new_list = [item for item in list_of_lists if item]
. 列表理解将返回一个不包含任何空列表的新列表。
# ✅ Remove empty lists from a list (using list comprehension) list_of_lists = [['one'], [], ['two'], [], ['three']] new_list = [item for item in list_of_lists if item] print(new_list) # 👉️ [['one'], ['two'], ['three']] # --------------------------------------------- # ✅ Remove empty lists from a list (using filter()) new_list = list(filter(None, list_of_lists)) print(new_list) # 👉️ [['one'], ['two'], ['three']] # --------------------------------------------- # ✅ Remove empty lists from a list (using for loop) for item in list_of_lists.copy(): if item == []: list_of_lists.remove(item) print(list_of_lists) # 👉️ [['one'], ['two'], ['three']]
第一个示例使用列表理解从列表列表中删除空列表。
list_of_lists = [['one'], [], ['two'], [], ['three']] new_list = [item for item in list_of_lists if item] print(new_list) # 👉️ [['one'], ['two'], ['three']]
在每次迭代中,我们检查当前项是否为真并返回结果。
空列表是虚假值,因此它们会被过滤掉。
列表理解创建一个新列表,它不会改变原始列表。
如果要从原始列表中删除空列表,请使用列表切片。
list_of_lists = [['one'], [], ['two'], [], ['three']] list_of_lists[:] = [item for item in list_of_lists if item] print(list_of_lists) # 👉️ [['one'], ['two'], ['three']]
我们使用my_list[:]
语法来获取代表整个列表的切片,因此我们可以直接分配给变量。
my_list[:]
代表整个列表,所以当我们在左侧使用它时,我们正在分配给整个列表。这种方法改变了原始列表的内容。
或者,您可以使用该filter()
功能。
使用 filter() 从列表列表中删除空列表
要从列表列表中删除空列表:
- 使用该
filter()
函数过滤掉空列表。 - 使用
list()
该类将filter
对象转换为列表。 - The new list won’t contain any empty lists.
list_of_lists = [['one'], [], ['two'], [], ['three']] new_list = list(filter(None, list_of_lists)) print(new_list) # 👉️ [['one'], ['two'], ['three']]
We used the filter()
function to remove the empty lists from a list of lists.
The filter function
takes a function and an iterable as arguments and constructs an iterator from
the elements of the iterable for which the function returns a truthy value.
None
for the function argument, all falsy elements of the iterable are removed.An empty list is a falsy value, so all empty lists get removed.
The last step is to use the list()
class to convert the filter
object to a
list.
Alternatively, you can use a for
loop.
Remove empty lists from a list of lists using for loop #
To remove the empty lists from a list of lists:
- Use a
for
loop to iterate over a copy of the list. - On each iteration, check if the current item is an empty list.
- Use the
list.remove()
method to remove the empty lists.
list_of_lists = [['one'], [], ['two'], [], ['three']] for item in list_of_lists.copy(): if item == []: list_of_lists.remove(item) print(list_of_lists) # 👉️ [['one'], ['two'], ['three']]
list.remove()
method to remove the matching elements.The
list.remove()
method removes the first item from the list whose value is equal to the passed
in argument.
The remove()
method mutates the original list and returns None
.
The most important thing to note when removing items from a list in a for
loop
is to use the list.copy()
method to iterate over a copy of the list.
If you try to iterate over the original list and remove items from it, you might
run into difficult to locate bugs.