Python built-in tools that make use of iteration protocol

In Python, any tool that employs the iteration protocol will automatically work on any built-in type or user-defined class that provides it. Basically, every built-in tool that scans from left to right across objects uses the iteration protocol.

The for loops

>>> for a in open('text.txt'):
...     print(a.upper(), end = '')
... 
HI THERE!
MY NAME IS DAN!
HEY, I AM A DEVELOPER.

List comprehensions and the map() built-in function

When applied to a file, they both leverage the file object’s iterator automatically to scan line by line, fetching an iterator with __iter__ and calling __next__ each time through remember, this is done automatically, you won’t see it in the code).

>>> U = [a.upper() for a in open('text.txt')]
>>> U
['HI THERE!\n', 'MY NAME IS DAN!\n', 'HEY, I AM A DEVELOPER.\n']
>>> 
>>> map(str.upper, open('text.txt'))

>>> list(map(str.upper, open('text.txt')))
['HI THERE!\n', 'MY NAME IS DAN!\n', 'HEY, I AM A DEVELOPER.\n']

Note:
The map() is a built-in that applies a function call to each item in the passed-in iterable object.

The sorted() sorts items in an iterable

>>> sorted(open('text.txt'))
['Hey, I am a developer.\n', 'Hi there!\n', 'My name is Dan!\n']

The zip() combines items from iterables

>>> list(zip(open('text.txt'), open('text.txt')))
[('Hi there!\n', 'Hi there!\n'), ('My name is Dan!\n', 'My name is Dan!\n'), ('Hey, I am a developer.\n', 'Hey, I am a developer.\n')]

The enumerate() pairs items in an iterable with relative positions

>>> list(enumerate(open('text.txt')))
[(0, 'Hi there!\n'), (1, 'My name is Dan!\n'), (2, 'Hey, I am a developer.\n')]

The filter() selects items for which a function is true

>>> # nonempty=True
>>> list(filter(bool, open('text.txt')))
['Hi there!\n', 'My name is Dan!\n', 'Hey, I am a developer.\n']

The reduce() runs pairs of items in an iterable through a function

>>> import functools, operator
>>> functools.reduce(operator.add, open('text.txt'))
'Hi there!\nMy name is Dan!\nHey, I am a developer.\n'

The list() and tuple() built-in functions

>>> list(open('text.txt'))
['Hi there!\n', 'My name is Dan!\n', 'Hey, I am a developer.\n']
>>> tuple(open('text.txt'))
('Hi there!\n', 'My name is Dan!\n', 'Hey, I am a developer.\n')

The string join() method

>>> '...'.join(open('text.txt'))
'Hi there!\n...My name is Dan!\n...Hey, I am a developer.\n'

The sequence assignment

>>> a, b, c = open('text.txt')
>>> a, b
('Hi there!\n', 'My name is Dan!\n')
>>> a, *b = open('text.txt')
>>> a, b
('Hi there!\n', ['My name is Dan!\n', 'Hey, I am a developer.\n'])

The in membership test

>>> 'My' in open('text.txt')
False
>>> 'My name is Dan!\n' in open('text.txt')
True

The slice assignment

>>> L = [1, 2, 3]
>>> L[:1] = open('text.txt')
>>> L
['Hi there!\n', 'My name is Dan!\n', 'Hey, I am a developer.\n', 2, 3]

The list’s extend() method

>>> L = [1, 2, 3]
>>> L.extend(open('text.txt'))
>>> L
[1, 2, 3, 'Hi there!\n', 'My name is Dan!\n', 'Hey, I am a developer.\n']

Note:
The extend() iterates but the append() does not.

The set() call, and the set and dictionary comprehension expressions

>>> set(open('text.txt'))
{'My name is Dan!\n', 'Hey, I am a developer.\n', 'Hi there!\n'}
>>>
>>> {line for line in open('text.txt')}
{'My name is Dan!\n', 'Hey, I am a developer.\n', 'Hi there!\n'}
>>>
>>> {ix: line for ix, line in enumerate(open('text.txt'))}
{0: 'Hi there!\n', 1: 'My name is Dan!\n', 2: 'Hey, I am a developer.\n'}

Note:
Both set and dictionary comprehensions support the extended syntax of list comprehensions, including if tests.

>>> {line for line in open('text.txt') if line[0] == 'H'}
{'Hey, I am a developer.\n', 'Hi there!\n'}

The range()

The range() returns an iterable that generates numbers in the range on demand.

>>> my_range = range(5)
>>> my_range
range(0, 5)

Range objects in Python 3.X support only iteration, indexing, and the len() function.

>>> I = iter(my_range)
>>> I

>>> next(I)
0
>>> next(I)
1
>>> next(I)
2

There are many other built-in functions that support the iteration protocol but many are not related to files or are difficult to exemplify through examples.

Noticeable exceptions are the built-ins max() and min() which return the largest and smallest item in an iterable, respectively.

>>> my_numbers = [7, 1, 9, 3, 5]
>>> max(my_numbers)
9
>>> min(my_numbers)
1

Here is the max() and min() applied to our file.

>>> max(open('text.txt'))
'My name is Dan!\n'
>>> min(open('text.txt'))
'Hey, I am a developer.\n'

Leave a Reply