📜  Python列表上的 Del、Remove 和 Pop 之间有什么区别?

📅  最后修改于: 2022-05-13 01:55:41.380000             🧑  作者: Mango

Python列表上的 Del、Remove 和 Pop 之间有什么区别?

在Python中del a 关键字和remove()、pop()是内置方法。这三个的目的相同但行为不同remove()方法使用 value 从列表中删除值或对象, delpop()使用索引从列表中删除值或对象。

德尔关键字:

del关键字从列表中删除任何变量、值列表。

句法:

del list_name[index]  # To delete single value
del list_name        # To delete whole list

例子:

Python3
# program to demonstrate use of del keyword
  
# assign list
numbers = [1, 2, 3, 2, 3, 4, 5]
  
# use del
del numbers[2]
  
# display list
print(numbers)
  
# use del
del numbers[-1]
  
# display list
print(numbers)
  
# use del
del numbers[0]
  
# display list
print(numbers)


Python3
# program to demonstrate use of remove() method
  
# assign list
numbers = [1, 2, 3, 2, 3, 4, 5]
  
# use remove()
numbers.remove(3)
  
# display list
print(numbers)
  
# use remove()
numbers.remove(2)
  
# display list
print(numbers)
  
# use remove()
numbers.remove(5)
  
# display list
print(numbers)


Python3
# program to demonstrate use of pop() method
  
# assign list
numbers = [1, 2, 3, 2, 3, 4, 5]
  
# use remove()
numbers.pop(3)
  
# display list
print(numbers)
  
# use remove()
numbers.pop(-1)
  
# display list
print(numbers)
  
# use remove()
numbers.pop(0)
  
# display list
print(numbers)


输出:

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

remove() 方法:

remove()方法从列表中删除第一个匹配值。

句法:

list_name.remove(value)

例子:

蟒蛇3

# program to demonstrate use of remove() method
  
# assign list
numbers = [1, 2, 3, 2, 3, 4, 5]
  
# use remove()
numbers.remove(3)
  
# display list
print(numbers)
  
# use remove()
numbers.remove(2)
  
# display list
print(numbers)
  
# use remove()
numbers.remove(5)
  
# display list
print(numbers)

输出:

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

pop() 方法:

像 del 一样的 pop() 方法删除特定索引处的值。但是 pop() 方法从列表中返回已删除的值。

句法:

list_name.pop(index)

例子:

蟒蛇3

# program to demonstrate use of pop() method
  
# assign list
numbers = [1, 2, 3, 2, 3, 4, 5]
  
# use remove()
numbers.pop(3)
  
# display list
print(numbers)
  
# use remove()
numbers.pop(-1)
  
# display list
print(numbers)
  
# use remove()
numbers.pop(0)
  
# display list
print(numbers)

输出:

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

del V/S remove() V/Spop()

                 del                

           remove()          

                 pop()             

del is a keyword.It is a method.pop() is a method.
To delete value it uses the index.To delete value this method uses the value as a parameter.This method also uses the index as a parameter to delete.
The del keyword doesn’t return any value.The remove() method doesn’t return any value.pop() returns deleted value.
The del keyword can delete the single value from a list or delete the whole list at a time.At a time it deletes only one value from the list.At a time it deletes only one value from the list.
It throws index error in case of the index doesn’t exist in the list.It throws value error in case of value doesn’t exist in the list.It throws index error in case of an index doesn’t exist in the list.