Home > Backend Development > Python Tutorial > Pitfalls in using for loop to delete elements in a list in python3

Pitfalls in using for loop to delete elements in a list in python3

不言
Release: 2018-04-19 11:28:45
Original
2330 people have browsed it

The following article will share with you a detailed discussion of the pitfalls of using a for loop to delete elements in a list in python3. It has a good reference value and I hope it will be helpful to everyone. Let’s take a look together

The object of the for loop statement is an iterable object. The iterable object needs to implement the __iter__ or iter method and return an iterator. What is an iterator? Iterators only need to implement the __next__ or next method.

Now let’s verify why the list supports iteration:


x = [1,2,3]
its = iter(x)
# its = x.__iter__()
print(type(its))
# print(its.__next__())
# print(its.__next__())
# print(its.__next__())
print(next(its))
print(next(its))
print(next(its))
Copy after login


Result:


<class &#39;list_iterator&#39;>
1
2
3
Copy after login


How does the for statement loop? The steps are:

(1) First determine whether the object is an iterable object. If not, report an error directly and throw a TypeError exception. If so, call the __iter__ or iter method and return an The iterator

(2) continuously calls the __next__ or next method of the iterator, returning a value in the iterator in order each time

(3) iterates to the end, no updates If there are multiple elements, an exception StopIteration will be thrown. Python will handle this exception by itself and will not expose it to developers


list1 = [1,2,3,4,5,6]
for i in list1:
 if i == 2:
  list1.remove(i)
 print(i)
print(list1)
Copy after login


Result:


##

1
2
4
5
6
[1, 3, 4, 5, 6]
Copy after login


The result printed by the second print is obviously that element 2 has been deleted list, why is there no 3 in the first print? Because when executing the for statement, an iterator is first generated, and then the next method is used to return the values ​​in the iterator in order. When element 2 is deleted from the list, element 3 The index becomes the index of the original element 2, and element 3 is pushed forward to the position of element 2, so the next method does not return the value 3. In python, you can use the iter function to get an ordered aggregate type iterator. Personally Understand the iterator as a one-way linked list with a next pointer. The obtained iterator is the header of the linked list, the header content is empty, and the next pointer points to the first element of the ordered aggregate type.

When accessing the next pointer of the last element of the linked list, python will report a StopIteration error.

Related recommendations:


Pitfalls to pay attention to when deleting elements from python list


The above is the detailed content of Pitfalls in using for loop to delete elements in a list in python3. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template