AMZ DIGICOM

Digital Communication

AMZ DIGICOM

Digital Communication

A Quick Tour of Python Iterators

PARTAGEZ

Python Iterators are Python objects with a quantifiable set of elements to implement the iterator protocol consisting of the two functions iterate() And next().

Python Iterators: what are they exactly?

Python Iterators are first a special form of Python objects. They are distinguished by their composition: they are in fact made up of a quantifiable set of elements. It is therefore possible to count the elements of an iterator and to iterate over all the elements of a Python Iterator.

Python is an excellent programming language, especially suitable for developing web projects. Use the Deploy Now tool for your web project and take advantage of GitHub’s auto-build and auto-deploy environment to get a bird’s-eye view of your project at all times.

Iterators or Iterables?

A Python Iterator should not be confused with a Python Iterable, even though the two are closely related. The different iterables, like the Python list, are distinguished by the use of a method iterate(), through which they can iterate. In order not to confuse them, you can reason as follows: all the elements positioned to the right of the loop header at the time of calling a for loop are iterables.

l = [1, 2, 3]
for x in l:
	print(x)

Python

As you can see, the list saved in the “l” variable appears to the right of the loop header, after the “in” keyword, when calling the for loop in Python; it is therefore an iterable.

It is possible to derive a Python Iterator of a Python Iterable. Below is a sample code to clarify this and better understand the differences between the two elements:

string = "test"
iteratorobject = iter(string)
next(iteratorobject)
iter(iteratorobject)

Python

In the code, a Python string containing the value test is saved in the variable called “string”. Strings also fall under the category of Iterables, because they can iterate through each letter in the string.

The fact that strings allow iterations actually implies that these strings also support the function iterate(). This is illustrated in the following line of code, where we create a Python Iterator by calling the function iterate() at the same time as the character string previously created. This returns a pointer to the string, which it stores in the variable called “iteratorobject”. The Python Iterator is therefore in a given state. This state can be modified by calling the method next() on the Python Iterator. The method moves the pointer one character, so that the iteratorobject value targets the first letter of the string after the function call.

The function call iterate(), which uses the Python Iterator as a pass parameter, returns a reference to it. Python Iterators are therefore self-iterable.

Iterators or Generators?

It is also essential to distinguish between Python Iterators and Python Generators. Whether each Python Generator is a Python Iterator, the converse is not necessarily true. Indeed, unlike a Python Generator, a Python Iterator is not necessarily created with a function including a yield expression.

Python Iterators are an advanced programming technique, which is not explained in all Python tutorials. If you want to discover other advanced techniques related to Python, we advise you to take a look at the following articles:

Use of Python Iterators: why and for what purpose?

Of course, Python Iterators are primarily used for the purpose ofiteration. The main advantage of Python Iterators lies in their operation, based on the principle of a “lazy evaluation(literally “lazy evaluation”). In other words, each element of a Python Iterator can be processed individually without having to load the entire data structure into memory. This solution has efficiency advantages, especially if you need to process large volumes of data where only one item needs to be loaded at a time.

Python Iterators: how to create them?

You can easily create your own Python Iterator. To do this, simply add the functions iterate() And next() to a Python object; this allows you to easily create a Python Iterator that returns all even numbers. The example below illustrates the procedure to follow:

class nombrespairs:
	def __iter__(self):
		self.x = 0
		return self

	def __next__(self):
		a = self.x
		self.x += 2
		return a

testobject = nombrespairs()
testiterator = iter(testobject)
print(next(testiterator))
print(next(testiterator))
print(next(testiterator))
print(next(testiterator))

Python

To create a Python Iterator, you must first create a class. Here it is called “even numbers”. Within this class, it is then necessary to implement the two functions iterate() And next(), each adopting the desired behavior.

In our case, the function iterate() simply returns a reference to the Python Iterator to host the sequence of integers (starting with 0). The logic of the iteration, i.e. the fact of returning only one number out of two (and therefore an even number) is taken care of by the function next().

Once the class is defined, an object of it is created and saved within the variable bearing the name “testobject”. This object becomes a Python Iterator when the iter() function is called, as we saw in the previous code example. Four calls to the next() function are then made, and their results are displayed on the screen. The result of the above section of code looks like this:

Python Iterators: how to limit them?

The Python Iterator responsible for iterating over even numbers could run forever, with even numbers technically having no end; it should therefore be limited. To do this, you can combine a StopIteration statement and an if…else statement in Python. Let’s say for example that you want to display in your Python Iterator all even numbers up to 100 (inclusive). The sample code below will guide you on how to proceed:

class nombrespairs:
	def __iter__(self):
		self.x = 0
		return self

	def __next__(self):
		if self.x <= 100:
			a = self.x
			self.x += 2
			return a	
		else:
			StopIteration

testobject = nombrespairs()
testiterator = iter(testobject)
print(next(testiterator))
print(next(testiterator))
print(next(testiterator))
print(next(testiterator))

Python

In the code, nothing has changed, except the implementation function next(). Here, an additional if…else statement has been added. It ensures that each number processed is indeed less than or equal to 100 and the iteration in the set of even numbers can only continue on this condition. As soon as the processed number exceeds 100, calling the StopIteration statement returns an error.

Télécharger notre livre blanc

Comment construire une stratégie de marketing digital ?

Le guide indispensable pour promouvoir votre marque en ligne

En savoir plus

Souhaitez vous Booster votre Business?

écrivez-nous et restez en contact