Python 简明教程

Python - Remove List Items

Removing List Items

在 Python 中删除列表项意味着从现有列表中删除元素。列表是有序的项集合,有时需要根据特定条件或索引从中删除某些元素。当我们删除列表项时,我们正在减小列表的大小或消除特定的元素。

Removing list items in Python implies deleting elements from an existing list. Lists are ordered collections of items, and sometimes you need to remove certain elements from them based on specific criteria or indices. When we remove list items, we are reducing the size of the list or eliminating specific elements.

我们可以使用各种方法在 Python 中删除 list 项,例如 remove()pop() 和 clear()。此外,我们可以使用 del 语句在特定索引处删除项。让我们通过本教程中的所有这些方法进行探索。

We can remove list items in Python using various methods such as remove(), pop() and clear(). Additionally, we can use the del statement to remove items at a specific index. Let us explore through all these methods in this tutorial.

Remove List Item Using remove() Method

Python 中的 remove() 方法用于从列表中删除指定项的首次出现。

The remove() method in Python is used to remove the first occurrence of a specified item from a list.

我们可以使用 remove() 方法删除列表项,方法是在括号中指定我们要删除的值,例如 my_list.remove(value) ,它会删除 my_list 中值的首次出现。

We can remove list items using the remove() method by specifying the value we want to remove within the parentheses, like my_list.remove(value), which deletes the first occurrence of value from my_list.

Example

在以下示例中,我们使用 remove() 方法从列表 “list1” 中删除元素 "Physics" -

In the following example, we are deleting the element "Physics" from the list "list1" using the remove() method −

list1 = ["Rohan", "Physics", 21, 69.75]
print ("Original list: ", list1)

list1.remove("Physics")
print ("List after removing: ", list1)

它将生成如下输出:

It will produce the following output −

Original list: ['Rohan', 'Physics', 21, 69.75]
List after removing: ['Rohan', 21, 69.75]

Remove List Item Using pop() Method

Python 中的 pop() 方法用于删除并返回列表中的最后一个元素(如果没有指定索引),或者删除并返回指定索引处的元素,从而更改原始列表。

The pop() method in Python is used to removes and returns the last element from a list if no index is specified, or removes and returns the element at a specified index, altering the original list.

我们可以使用 pop() 方法删除列表项,方法是在不带任何参数的情况下调用它 my_list.pop() ,它会删除并返回 my_list 中的最后一个项,或者通过提供我们要删除项 my_list.pop(index) 的索引,它会删除并返回该索引处的项。

We can remove list items using the pop() method by calling it without any arguments my_list.pop(), which removes and returns the last item from my_list, or by providing the index of the item we want to remove my_list.pop(index), which removes and returns the item at that index.

Example

以下示例显示如何使用 pop() 方法删除列表项 −

The following example shows how you can use the pop() method to remove list items −

list2 = [25.50, True, -55, 1+2j]
print ("Original list: ", list2)
list2.pop(2)
print ("List after popping: ", list2)

我们得到了如下输出 −

We get the output as shown below −

Original list: [25.5, True, -55, (1+2j)]
List after popping: [25.5, True, (1+2j)]

Remove List Item Using clear() Method

Python 中的 clear() 方法用于从列表中删除所有元素,使其为空。

The clear() method in Python is used to remove all elements from a list, leaving it empty.

我们可以使用 clear() 方法删除所有列表项,方法是在列表对象上调用它,例如 my_list.clear() ,它将清空 my_list ,使其不包含任何元素。

We can remove all list items using the clear() method by calling it on the list object like my_list.clear(), which empties my_list, leaving it with no elements.

Example

在此示例中,我们使用 clear() 方法从列表 “my_list” 中删除所有元素 -

In this example, we are using the clear() method to remove all elements from the list "my_list" −

my_list = [1, 2, 3, 4, 5]

# Clearing the list
my_list.clear()
print("Cleared list:", my_list)

上述代码的输出如下:

Output of the above code is as follows −

Cleared list: []

Remove List Item Using del Keyword

Python 中的 del 关键字用于从内存中删除特定索引处的元素或索引切片。

The del keyword in Python is used to delete element either at a specific index or a slice of indices from memory.

我们可以使用 del 关键字删除列表项,方法是指定我们要删除的项的索引或切片,例如 del my_list[index] 删除单个项或 del my_list[start:stop] 删除一系列项。

We can remove list items using the del keyword by specifying the index or slice of the items we want to delete, like del my_list[index] to delete a single item or del my_list[start:stop] to delete a range of items.

Example

在以下示例中,我们使用 “del” 关键字从列表 “list1” 中删除索引 “2” 处的元素 -

In the below example, we are using the "del" keyword to delete an element at the index "2" from the list "list1" −

list1 = ["a", "b", "c", "d"]
print ("Original list: ", list1)
del list1[2]
print ("List after deleting: ", list1)

产生的结果如下 −

The result produced is as follows −

Original list: ['a', 'b', 'c', 'd']
List after deleting: ['a', 'b', 'd']

Example

在此处,我们使用切片运算符从列表中删除一系列连续项 -

In here, we are deleting a series of consecutive items from a list with the slicing operator −

list2 = [25.50, True, -55, 1+2j]
print ("List before deleting: ", list2)
del list2[0:2]
print ("List after deleting: ", list2)

它将生成如下输出:

It will produce the following output −

List before deleting: [25.5, True, -55, (1+2j)]
List after deleting: [-55, (1+2j)]