从 Python 列表的元素中删除 [clear, pop, remove, del]
Python 列表数据类型可帮助您以有序的序列存储不同数据类型的项。数据写在方括号 [] 中,值用逗号 (,) 分隔。
在 Python 中,列表数据类型有许多可用方法,可以帮助您从给定列表中删除元素。这些方法是 remove()、pop() 和 clear()。
除了列表方法,您还可以使用 del 关键字从列表中删除项。
列表示例
my_list = ['Guru', 50, 11.50, 'Siya', 50, ['A', 'B', 'C']]
索引从 0 开始。在列表中:my_list 在
索引 0 处是字符串 'Guru',
- 索引 1 处是数字 50,这是一个整数。
- 索引 2 处是浮点数 11.50
- 索引 3 处有一个字符串 'Siya.'。
- 索引 4 处,您将看到数字 50 重复出现。
- 索引 5 处,您将得到一个包含 A、B 和 C 值的列表。
Python remove() 方法
Python remove() 方法是列表自带的内置方法。它有助于从列表中删除匹配到的第一个元素。
语法
list.remove(element)
您想从列表中删除的元素。
返回值
此方法没有返回值。
使用 remove() 方法的提示
以下是使用 remove() 方法时需要记住的重要几点:
- 当列表中存在重复元素时,将从列表中删除与给定元素匹配的第一个元素。
- 如果列表中不存在给定元素,则会引发错误,提示“列表不存在该元素”。
- remove() 方法不返回任何值。
- remove() 将值作为参数,因此必须传递具有正确数据类型的值。
示例:使用 remove() 方法从列表中删除元素
这是我有一个的示例列表
my_list = [12, 'Siya', 'Tiya', 14, 'Riya', 12, 'Riya']
该列表包含字符串和数字数据类型的元素。列表包含重复的元素,如数字 12 和字符串 Riya。
my_list = [12, 'Siya', 'Tiya', 14, 'Riya', 12, 'Riya'] my_list.remove(12) # it will remove the element 12 at the start. print(my_list) my_list.remove('Riya') # will remove the first Riya from the list print(my_list) my_list.remove(100) #will throw an error print(my_list)
输出:
['Siya', 'Tiya', 14, 'Riya', 12, 'Riya'] ['Siya', 'Tiya', 14, 12, 'Riya'] Traceback (most recent calllast): File "display.py", line 9, in <module> my_list.remove(100) ValueError: list.remove(x): x not in the list
Python pop() 方法
pop() 方法根据给定的索引从列表中删除元素。
语法
list.pop(index)
索引:pop() 方法只有一个名为 index 的参数。
- 要从列表中删除元素,您需要传递元素的索引。索引从 0 开始。要获取列表中的第一个元素,请传递索引 0。要删除最后一个元素,您可以传递索引 -1。
- index 参数是可选的。如果未传递,则默认值为 -1,并将返回列表中的最后一个元素。
- 如果给定的索引不存在或超出范围,pop() 方法将引发错误,提示 IndexError: pop index.
返回值
pop() 方法将根据给定的索引返回已删除的元素。最终列表也会更新,不会包含该元素。
示例:使用 pop() 方法从列表中删除元素
示例中将使用的列表是 my_list = [12, ‘Siya’, ‘Tiya’, 14, ‘Riya’, 12, ‘Riya’]。
让我们尝试根据以下方式使用 pop() 方法删除元素:
- 通过提供索引
- 不带索引
- 传递超出范围的索引。
这里,我们正在从列表中删除 Tiya。索引从 0 开始,所以 Tiya 的索引是 2。
my_list = [12, 'Siya', 'Tiya', 14, 'Riya', 12, 'Riya'] #By passing index as 2 to remove Tiya name = my_list.pop(2) print(name) print(my_list) #pop() method without index – returns the last element item = my_list.pop() print(item) print(my_list) #passing index out of range item = my_list.pop(15) print(item) print(my_list)
输出:
Tiya [12, 'Siya', 14, 'Riya', 12, 'Riya'] Riya [12, 'Siya', 14, 'Riya', 12] Traceback (most recent calllast): File "display.py", line 14, in <module> item = my_list.pop(15) IndexError: popindex out of range
Python clear() 方法
clear() 方法将删除列表中的所有元素。
语法
list.clear()
参数
无参数。
返回值
没有返回值。使用 clear() 方法将清空列表。
示例:使用 clear() 方法删除列表中的所有元素
clear() 方法将清空给定的列表。让我们在下面的示例中看看 clear() 的工作方式。
my_list = [12, 'Siya', 'Tiya', 14, 'Riya', 12, 'Riya'] #Using clear() method element = my_list.clear() print(element) print(my_list)
输出:
None []
使用 del 关键字
要从列表中删除元素,您可以使用 del 关键字后跟列表。您必须将元素的索引传递给列表。索引从 0 开始。
语法
del list[index]
您还可以使用 del 关键字从列表中切片一个元素范围。可以为 del 关键字提供列表的开始/停止索引,并且位于该范围内的元素将被删除。语法如下:
语法
del list[start:stop]
下面是一个示例,展示了如何使用 del 删除列表中的第一个元素、最后一个元素和多个元素。
my_list = list(range(15)) print("The Original list is ", my_list) #To remove the firstelement del my_list[0] print("After removing first element", my_list) #To remove last element del my_list[-1] print("After removing last element", my_list) #To remove element for given index : for example index:5 del my_list[5] print("After removing element from index:5", my_list) #To remove last 2 elements from the list del my_list[-2] print("After removing last 2 elements", my_list) #To remove multiple elements delmy_list[1:5] print("After removing multiple elements from start:stop index (1:5)", my_list) #To remove multiple elements del my_list[4:] print("To remove elements from index 4 till the end (4:)", my_list)
输出:
The Originallist is [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14] After removing first element [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14] After removing last element [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13] After removing element from index:5 [1, 2, 3, 4, 5, 7, 8, 9, 10, 11, 12, 13] After removing last 2 elements [1, 2, 3, 4, 5, 7, 8, 9, 10, 11, 13] After removing multiple elements from start:stop index (1:5) [1, 7, 8, 9, 10, 11, 13] To remove elements from index 4 till the end (4:) [1, 7, 8, 9]
如何在列表中删除第一个元素?
您可以使用列表方法 remove()、pop() 来删除列表中的第一个元素。对于 remove() 方法,您需要传递要删除的第一个元素,对于 pop() 则传递索引,即 0。
您也可以使用 del 关键字从列表中删除第一个元素。
下面的示例展示了如何使用 remove()、pop() 和 del 从列表中删除第一个元素。
my_list1 = ['A', 'B', 'C', 'D', 'E', 'F'] print("The Originallist is ", my_list1) #Using remove() to remove first element my_list1.remove('A') print("Using remove(), the final list is ", my_list1) my_list1 = ['A', 'B', 'C', 'D', 'E', 'F'] print("The Originallist is ", my_list1) #Using pop() to remove the first element element = my_list1.pop(0) print("The first element removed from my_list1 is ", element) print("Using pop(), the final list is ", my_list1) #Using del to remove the first element my_list2 = ['A', 'B', 'C', 'D', 'E', 'F'] del my_list2[0] print("Using del, the final list is ", my_list2)
输出:
The Originallist is ['A', 'B', 'C', 'D', 'E', 'F'] Using remove(), the final list is ['B', 'C', 'D', 'E', 'F'] The Originallist is ['A', 'B', 'C', 'D', 'E', 'F'] The first element removed from my_list1 is A Using pop(), the final list is ['B', 'C', 'D', 'E', 'F'] Using del, the final list is ['B', 'C', 'D', 'E', 'F']
如何在 Python 列表中删除多个元素?
列表方法 remove() 和 pop() 用于删除单个元素。要删除多个项,请使用 del 关键字。
从列表 [‘A’, ‘B’, ‘C’, ‘D’, ‘E’, ‘F’] 中,我们要删除元素 B、C 和 D。下面的示例展示了如何使用 del 关键字来删除这些元素。
#Using del to remove the multiple elements from list my_list2 = ['A', 'B', 'C', 'D', 'E', 'F'] print("Originallist is ", my_list2) del my_list2[1:4] print("Using del, the final list is ", my_list2)
输出:
Originallist is ['A', 'B', 'C', 'D', 'E', 'F'] Using del, the final list is ['A', 'E', 'F']
如何在 Python 中通过索引删除列表中的元素?
要根据索引删除元素,您可以使用列表方法 pop()。使用 del 关键字也可以帮助您删除给定索引处的元素。
#Using del to remove the multiple elements from list my_list1 = ['A', 'B', 'C', 'D', 'E', 'F'] print("Originallist is ", my_list1) element = my_list1.pop(2) print("Element removed for index: 2 is ", element) print("Using pop, the final list is ", my_list1) #Using del to remove the multiple elements from list my_list2 = ['A', 'B', 'C', 'D', 'E', 'F'] print("Originallist is ", my_list2) del my_list2[2] print("Using del, the final list is ", my_list2)
输出
Originallist is ['A', 'B', 'C', 'D', 'E', 'F'] Element removed for index: 2 is C Using pop, the final list is ['A', 'B', 'D', 'E', 'F'] Originallist is ['A', 'B', 'C', 'D', 'E', 'F'] Using del, the final list is ['A', 'B', 'D', 'E', 'F']
摘要
在 Python 中,列表数据类型有许多可用方法,可以帮助您从给定列表中删除元素。这些方法是 remove()、pop() 和 clear()。
用于删除列表元素的内置重要方法
方法 | 描述 |
---|---|
remove() | 它有助于从列表中删除匹配到的第一个给定元素。 |
pop() | pop() 方法根据给定的索引从列表中删除元素。 |
clear() | clear() 方法将删除列表中的所有元素。 |