从中删除元素 Python 列表 [清除,弹出,移除,删除]

Python 列表数据类型可帮助您按顺序存储不同数据类型的项目。数据写在方括号 ([]) 内,值以逗号 (,) 分隔。

In Python,列表数据类型上有许多方法可帮助您从给定列表中删除元素。这些方法包括 移除 ()、弹出 ()明确() .

除了列表方法之外,你还可以使用 关键字从列表中删除项目。

列表示例

my_list = ['Guru', 50, 11.50, 'Siya', 50, ['A', 'B', 'C']]

索引从 0 开始。在列表中:my_list 位于

0th 索引中有字符串 'Guru',

  • 在索引:1 处,您将得到整数 50。
  • 在索引:2 处,您将获得浮点数 11.50
  • 在索引:3 处有一个字符串“Siya”。
  • 在索引:4 处,您将看到数字 50 重复。
  • 在索引:5 处,您将获得一个包含值 A、B 和 C 的列表。

Python 删除()方法

Python removes() 方法是列表的内置方法。它有助于从列表中删除给定的第一个匹配元素。
语法:

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)

index:pop() 方法只有一个名为index的参数。

  • 要从列表中删除一个元素,您需要传递元素的索引。索引从 0 开始。要从列表中获取第一个元素,请将索引传递为 0。要删除最后一个元素,您可以将索引传递为 -1。
  • index 参数是可选的。如果未传递,则默认值为 -1,并返回列表中的最后一个元素。
  • 如果给定的索引不存在或超出范围,pop()方法将抛出错误,提示 IndexError:弹出索引。

返回值:

pop() 方法将根据给定的索引返回被移除的元素。最终列表也会更新,并且不会包含该元素。

示例:使用 pop() 方法从列表中删除元素

示例中将使用的列表是 my_list = [12, 'Siya', 'Tiya', 14, 'Riya', 12, 'Riya'] 。

让我们尝试使用基于以下内容的 pop() 方法删除元素:

  • 通过给出索引
  • 无索引
  • 传递超出范围的索引。

在这里,我们正在删除 提雅 从列表中。索引从 0 开始,因此 提雅 是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() 方法清空 list()。

示例:使用 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 关键字

要从列表中删除元素,可以使用 关键字后跟一个列表。您必须将元素的索引传递给列表。索引从 0 开始。
语法:

del list[index]

您还可以使用以下方法从列表中切出一定范围内的元素: 关键字。可以将列表中的起始/终止索引赋予 del 关键字,并且将删除属于该范围的元素。语法如下:
语法:

del list[start:stop]

下面是一个示例,演示如何使用以下方法从列表中删除第一个元素、最后一个元素以及多个元素 .

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() 方法,您必须传递要删除的第一个元素,并弹出索引,即 0。

您也可以使用 关键字从列表中删除第一个元素。

下面的示例显示使用 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() 用于删除单个元素。要删除多个方面,请使用 德尔 关键词。

从列表 ['A', 'B', 'C', 'D', 'E', 'F'] 中,我们想要删除元素 B、C 和 D。下面的示例显示了如何使用 关键字来删除元素。

#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() 。即使使用 关键字将帮助您删除给定索引的元素。

#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']

总结

In Python,列表数据类型上有许多方法可帮助您从给定列表中删除元素。这些方法包括 移除 ()、弹出 ()清除()。

列表中提供了重要的内置方法来删除元素

付款方式 描述
去掉() 它有助于从列表中删除第一个匹配的元素。
流行音乐() pop() 方法根据给定的索引从列表中删除一个元素。
明确() clear() 方法将删除列表中的所有元素。

每日Guru99新闻简报

通过立即获取最新、最重要的人工智能新闻报道来开始您的一天。