9.10. Iterator Product

  • itertools.product(*iterables, repeat=1)

9.10.1. Solution

>>> from itertools import product
>>>
>>>
>>> data = product(['a', 'b', 'c'], [1,2])
>>>
>>> next(data)
('a', 1)
>>> next(data)
('a', 2)
>>> next(data)
('b', 1)
>>> next(data)
('b', 2)
>>> next(data)
('c', 1)
>>> next(data)
('c', 2)
>>> next(data)
Traceback (most recent call last):
StopIteration

9.10.2. Case Study

>>> from itertools import product
>>> from pprint import pprint
>>> filenames = ['file1.txt', 'file2.txt']
>>> modes = ['r', 'w', 'a']
>>> encodings = ['utf-8', None, 'iso-8859-1']
>>> result = product(filenames, modes, encodings)
>>> pprint(list(result))
[('file1.txt', 'r', 'utf-8'),
 ('file1.txt', 'r', None),
 ('file1.txt', 'r', 'iso-8859-1'),
 ('file1.txt', 'w', 'utf-8'),
 ('file1.txt', 'w', None),
 ('file1.txt', 'w', 'iso-8859-1'),
 ('file1.txt', 'a', 'utf-8'),
 ('file1.txt', 'a', None),
 ('file1.txt', 'a', 'iso-8859-1'),
 ('file2.txt', 'r', 'utf-8'),
 ('file2.txt', 'r', None),
 ('file2.txt', 'r', 'iso-8859-1'),
 ('file2.txt', 'w', 'utf-8'),
 ('file2.txt', 'w', None),
 ('file2.txt', 'w', 'iso-8859-1'),
 ('file2.txt', 'a', 'utf-8'),
 ('file2.txt', 'a', None),
 ('file2.txt', 'a', 'iso-8859-1')]
class NumbersTest(unittest.TestCase):

    def test_even(self):
        """
        Test that numbers between 0 and 5 are all even.
        """
        for i in range(0, 6):
            with self.subTest(i=i):
                self.assertEqual(i % 2, 0)

9.10.3. Further Reading

9.10.4. Assignments

# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author

# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -v myfile.py`

# %% About
# - Name: Idiom Product Generate
# - Difficulty: easy
# - Lines: 1
# - Minutes: 3

# %% English
# 1. Define variable `result` with generated test data
# 2. Use `itertools.product()` function
# 3. Run doctests - all must succeed

# %% Polish
# 1. Zdefiniuj zmienną `result` z wygenerowanymi danymi testowymi
# 2. Użyj funkcji `itertools.product()`
# 3. Uruchom doctesty - wszystkie muszą się powieść

# %% Tests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'

>>> assert result is not Ellipsis, \
'Assign result to variable: `result`'

>>> assert type(result) is product, \
'Variable `result` has invalid type, should be product'

>>> result = list(result)

>>> from pprint import pprint
>>> pprint(result, width=72, sort_dicts=False)
[('Mark', 'Watney', 'botanist'),
 ('Mark', 'Watney', 'commander'),
 ('Mark', 'Watney', 'pilot'),
 ('Mark', 'Lewis', 'botanist'),
 ('Mark', 'Lewis', 'commander'),
 ('Mark', 'Lewis', 'pilot'),
 ('Mark', 'Martinez', 'botanist'),
 ('Mark', 'Martinez', 'commander'),
 ('Mark', 'Martinez', 'pilot'),
 ('Melissa', 'Watney', 'botanist'),
 ('Melissa', 'Watney', 'commander'),
 ('Melissa', 'Watney', 'pilot'),
 ('Melissa', 'Lewis', 'botanist'),
 ('Melissa', 'Lewis', 'commander'),
 ('Melissa', 'Lewis', 'pilot'),
 ('Melissa', 'Martinez', 'botanist'),
 ('Melissa', 'Martinez', 'commander'),
 ('Melissa', 'Martinez', 'pilot'),
 ('Rick', 'Watney', 'botanist'),
 ('Rick', 'Watney', 'commander'),
 ('Rick', 'Watney', 'pilot'),
 ('Rick', 'Lewis', 'botanist'),
 ('Rick', 'Lewis', 'commander'),
 ('Rick', 'Lewis', 'pilot'),
 ('Rick', 'Martinez', 'botanist'),
 ('Rick', 'Martinez', 'commander'),
 ('Rick', 'Martinez', 'pilot')]
"""
from itertools import product


FIRSTNAMES = ['Mark', 'Melissa', 'Rick']
LASTNAMES = ['Watney', 'Lewis', 'Martinez']
ROLES = ['botanist', 'commander', 'pilot']


# Define variable `result` with generated test data
# Use `itertools.product()` function
# type: list[tuple[str,...]]
result = ...