concept iterator in category python

appears as: n iterator, iterators, iterator, iterator, An iterator
Python Workout: 50 ten-minute exercises

This is an excerpt from Manning's book Python Workout: 50 ten-minute exercises.

Table 1.1 What you need to know (view table figure)

Concept

What is it?

Example

To learn more

random

Module for generating random numbers and selecting random elements

number = random.randint(1, 100)

http://mng.bz/Z2wj

Comparisons

Operators for comparing values

x < y

http://mng.bz/oPJj

f-strings

Strings into which expressions can be interpolated

f'It is currently {datetime.datetime .now()}'

http://mng.bz/1z6Z and http://mng.bz/PAm2

for loops

Iterates over the elements of an iterable

for i in range(10): print(i*i)

http://mng.bz/Jymp

input

Prompts the user to enter a string, and returns a string

input('Enter your name: ')

http://mng.bz/wB27

enumerate

Helps us to number elements of iterables

for index, item in enumerate('abc'): print(f'{index}: {item}')

http://mng.bz/qM1K

reversed

Returns an iterator with the reversed elements of an iterable

r = reversed('abcd')

http://mng.bz/7XYx

Iterators are pervasive in Python because they’re so convenient--and in many ways, they’ve been made convenient because they’re pervasive. In this chapter, you’ll practice writing all of these types of iterators and getting a feel for when each of these techniques should be used.

Mastering Large Datasets with Python: Parallelize and Distribute Your Python Code

This is an excerpt from Manning's book Mastering Large Datasets with Python: Parallelize and Distribute Your Python Code.

Iterators are the base class of all the Python data types that can be iterated over. That is, we can loop over the items of an iterator, or we can map a function across one, like we learned how to do in chapter 2. The iteration process is defined by a special method called .__iter__(). If a class has this method and returns an object with a .__next__() method, then we can iterate over it.

4.3. Understanding iterators: The magic behind lazy Python

So far, we’ve talked about the benefits of laziness and about a couple functions that can take advantage of them. In this section, we’ll dig into the details of iterators—objects that we can move through in sequence—and talk about generators—special functions for creating sequences. We touched on generators briefly in chapter 2, but this time we’ll dive even deeper, including a look at small generator expressions.

It’s important that we understand how iterators work because they are fundamental to our ability to process big data on our laptop or desktop computer. We use iterators to replace data with instructions about where to find data and to replace transformations with instructions for how to execute those transformations. This substitution means that the computer only has to concern itself with the data it is processing right now, as opposed to the data it just processed or has to process in the future.

Hello! Python

This is an excerpt from Manning's book Hello! Python.

Iterators are a common solution to a frequent programming task: you have a bunch of things—now how do you do something to each one of them? The trick is that most Python collections, be they lists, files, or sets, can be used as iterators.

The interface of an iterator is straightforward. It has a .next() method, which you call over and over again to get each value in the sequence. Once all the values are gone, the iterator raises a StopIteration exception, which tells Python to stop looping. In practice, using an iterator looks something like figure 7.2.

Figure 7.2. The iterator protocol: once you run through three iterations, it stops.

When you ask Python to iterate over an object such as a list, the first thing it does is call iter(object), which calls that object’s __iter__ method and expects to get an iterator object. You don’t need to use the __iter__ call directly unless you’re creating your own custom iterator. In that case, you’ll need to implement both the __iter__ and the next() methods yourself. This listing shows how to use the iter function to create an iterator from any iterable Python object.

Listing 7.8. Using an iterator the hard way