Learn more about pop and remove in Python

  • 2020-06-23 01:06:32
  • OfStack

Python There are generally two ways to remove an element from list, pop() and remove().

remove() The function removes the first match of a value in the list.

remove() Method syntax:

list.remove(obj)

pop() The function removes 1 element from the list (the default is the last element) and returns the value of that element.

pop() Method syntax:

list.pop(obj=list[-1])

If you remove a single element, there is little problem with using it, as follows.

1.pop() method, which passes index of the element to be deleted:


x = ['a', 'b', 'c', 'd']
x.pop(2)
print x
 
------------------
result:
['a', 'b', 'd']

2. remove() passes the element to be deleted. If there are more than one element, the first element is deleted by default:


x = ['a', 'b', 'a', 'c', 'd']
x.remove('a')
print x
 
-----------------
result : 
['b', 'a', 'c', 'd']

If you want to iterate over elements that meet certain criteria, use with caution!!


x = ['a', 'b', 'c', 'd']
y = ['b', 'c']
for i in x:
  if i in y:
    x.remove(i)
print x
 
-----------------------
result : 
['a', 'c', 'd']

x = ['a', 'b', 'c', 'd']
y = ['b', 'c']
for i in x:
  if i in y:
    idx = x.index(i)
    x.pop(idx)
print x
 
--------------
result : 
['a', 'c', 'd']

I think the main reason for this is that the pop and remove methods are 'destructive operations' (ps: forgive me for creating the definition). After x.remove (), the original location of x in the memory has been freed and the new x has been reapplied for. It can be understood that x is no longer the original x, and the x passed in the for loop is still the location of the original x in memory, so after x.remove (i), the for loop cannot find x, and the deletion cannot be completed. To complete the problem of looping out list elements, I recommend the following approach.

Thank you maybe a bit small remind, the interpretation of the original is wrong, the cause of the above phenomenon is because, in the Python for i in list is to use an iterator implementation, hidden inside the record the current state of the iterator, and remove method to delete the current element, automatic iterator refers to the next one element, the original list in memory location has not changed. This can be avoided in the following ways.


x = ['a', 'b', 'c', 'd']
y = ['b', 'c']
x_new = []
for i in x:
  if i not in y:
    x_new.append(i)
x = x_new
print x
 
----------------------
result : 
['a', 'd']

At the same time, coco Coco 1 proposed to change for i in x: to for i in x[:]: can also be realized, because x[:] and x is not the same as list, it is equivalent to copy the memory of x to a new memory, when x remove operation, the new memory of list is not affected.

conclusion

That is the end of this article on how to use pop and remove in Python. I hope you will find it helpful. Those who are interested can continue to see this site:

Introduction to Python: The Full Solution of the three-angle Function

A Quick Look at several Functions in Python

Analysis of python Regular Expression re's compile Function

If there is any deficiency, please let me know. Thank you for your support!


Related articles: