Detailed explanation of Python list pop() function

"Author's Homepage": Shibie Sanri wyx
"Author's Profile": CSDN top100, Alibaba Cloud Blog Expert, Huawei Cloud Share Expert, Network Security High-quality Creator
"Recommended Column": Xiaobai Zero Basic "Python Beginner to Master"

pop() can "delete" an element in the list (the last one by default).

grammar

list.pop( index )

parameter

  • index : (optional) the index value of the element to be removed

return value

  • return the removed element

Example: delete the first element in the list

list1 = [1, 2, 3]

list1.pop(1)
print(list1)

output:

[1, 3]

1. Delete elements by index

pop() can delete the element corresponding to the "position" according to the specified "index" .

1.1. Positive index

When the index is a "positive number" , start from 0 and delete in order from left to right

list1 = [1, 2, 3, 4]

print('删除前:', list1)
list1.pop(0)
print('删除后:', list1)

output:

删除前: [1, 2, 3, 4]
删除后: [2, 3, 4]

The specified index cannot exceed the "length" of the list , otherwise an error will be reported IndexError: pop index out of range

insert image description here


1.2. Negative index

When the index is a "negative number" , start from 1 and delete in order from right to left

list1 = [1, 2, 3, 4]

print('删除前:', list1)
list1.pop(-1)
print('删除后:', list1)

output:

删除前: [1, 2, 3, 4]
删除后: [1, 2, 3]

When the index exceeds the "length" of the list , an error will also be reported IndexError: pop index out of range

insert image description here

1.3. No index specified

When "do not specify an index" , the default is -1, that is, delete the last element

list1 = [1, 2, 3, 4]

print('删除前:', list1)
list1.pop()
print('删除后:', list1)

output:

删除前: [1, 2, 3, 4]
删除后: [1, 2, 3]

2. Return the deleted element

pop() can be understood as "popping" elements, it will return the deleted elements, we can print the deleted elements to judge whether there is a deletion error.

list1 = [1, 2, 3, 4]

print(list1.pop())

output:

4

3. Different types of elements

In the above case, we delete one element, which is easy to understand.

For cases such as "nested" lists in lists , the list as a whole will be deleted as an element, such as the following:

list1 = [1, 2, [1, 2, 3], 4]

print('删除前:', list1)
list1.pop(2)
print('删除前:', list1)

output:

删除前: [1, 2, [1, 2, 3], 4]
删除前: [1, 2, 4]

When the tuples and dictionaries are nested in the list, they are also deleted entirely

list1 = [1, 2, {
    
    1, 2, 3}, 4]

print('删除前:', list1)
list1.pop(2)
print('删除前:', list1)

output:

删除前: [1, 2, {
    
    1, 2, 3}, 4]
删除前: [1, 2, 4]

Even if there are many layers of nesting, it will be deleted entirely

list1 = [1, 2, [1, [1, [1, 2]]], 4]

print('删除前:', list1)
list1.pop(2)
print('删除前:', list1)

output:

删除前: [1, 2, [1, [1, [1, 2]]], 4]
删除前: [1, 2, 4]

4. Common mistakes

The pop() of the list can only delete one element at a time, otherwise it will report an error TypeError: pop expected at most 1 argument

insert image description here

The parameter of pop() must be int, and elements can only be deleted according to the index, otherwise an error will be reported TypeError: 'str' object cannot be interpreted as an integer

insert image description here

Guess you like

Origin blog.csdn.net/wangyuxiang946/article/details/131595288