Iterating over a collection using a for
loop in Python relies on iterators.
An iterator is an object that allows you to traverse a collection of elements, such as a list or a dictionary. Iterators are used in
for
loops to iterate over the elements of a collection one at a time.
When you create an iterator, it keeps track of the current position in the collection and provides a way to access the next element. The
next()
function is used to retrieve the next element from the iterator. When there are no more elements to iterate over, the
next()
function raises a StopIteration exception and the iteration stops.
It is important to note that iterators are designed to be read-only. Modifying a collection while iterating over it can cause unexpected behavior,
as the iterator may skip over or repeat elements. A RuntimeError
may also be raised in this situation, with the message changed
size during iteration
. Therefore, it is important to avoid modifying a collection while iterating over it to ensure that your code behaves as
expected.
If you still want to modify the collection, it is best to use a second collection or to iterate over a copy of the original collection instead.
Code examples
Noncompliant code example
def my_fun():
my_dict = {'a': 1, 'b': 2, 'c': 3}
for key in my_dict:
if my_dict[key] == 'foo':
my_dict.pop(key) # Noncompliant: this will make the iteration unreliable
Compliant solution
def my_fun():
my_dict = {'a': 1, 'b': 2, 'c': 3}
for key in list(my_dict.keys()):
if my_dict[key] == 'foo':
my_dict.pop(key)