9.9. Iterator Permutations

  • itertools.permutations(iterable, r=None)

9.9.1. Solution

>>> from itertools import permutations
>>>
>>>
>>> data = permutations([1,2,3])
>>>
>>> next(data)
(1, 2, 3)
>>> next(data)
(1, 3, 2)
>>> next(data)
(2, 1, 3)
>>> next(data)
(2, 3, 1)
>>> next(data)
(3, 1, 2)
>>> next(data)
(3, 2, 1)
>>> next(data)
Traceback (most recent call last):
StopIteration

9.9.2. Case Study

>>> from itertools import permutations
>>> from pprint import pprint
>>>
>>>
>>> actions = ['play', 'stop', 'rewind', 'forward']
>>>
>>> result = permutations(actions)
>>> pprint(list(result))
[('play', 'stop', 'rewind', 'forward'),
 ('play', 'stop', 'forward', 'rewind'),
 ('play', 'rewind', 'stop', 'forward'),
 ('play', 'rewind', 'forward', 'stop'),
 ('play', 'forward', 'stop', 'rewind'),
 ('play', 'forward', 'rewind', 'stop'),
 ('stop', 'play', 'rewind', 'forward'),
 ('stop', 'play', 'forward', 'rewind'),
 ('stop', 'rewind', 'play', 'forward'),
 ('stop', 'rewind', 'forward', 'play'),
 ('stop', 'forward', 'play', 'rewind'),
 ('stop', 'forward', 'rewind', 'play'),
 ('rewind', 'play', 'stop', 'forward'),
 ('rewind', 'play', 'forward', 'stop'),
 ('rewind', 'stop', 'play', 'forward'),
 ('rewind', 'stop', 'forward', 'play'),
 ('rewind', 'forward', 'play', 'stop'),
 ('rewind', 'forward', 'stop', 'play'),
 ('forward', 'play', 'stop', 'rewind'),
 ('forward', 'play', 'rewind', 'stop'),
 ('forward', 'stop', 'play', 'rewind'),
 ('forward', 'stop', 'rewind', 'play'),
 ('forward', 'rewind', 'play', 'stop'),
 ('forward', 'rewind', 'stop', 'play')]

9.9.3. Further Reading

9.9.4. Use Case - 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.9.5. Use Case - 2

>>> from unittest import TestCase
>>> from itertools import permutations
>>>
>>>
>>> def click(button1, button2, button3, button4):
...     return ...
>>>
>>>
>>> class PayerTest(TestCase):
...     def test_buttons(self):
...         buttons = ['play', 'stop', 'rev', 'fwd']
...         for order in permutations(buttons):
...             with self.subTest(order):
...                 result = click(*order)
...                 self.assertEqual(result, ...)

9.9.6. 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 Permutations Generate
# - Difficulty: easy
# - Lines: 1
# - Minutes: 3

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

# %% Polish
# 1. Zdefiniuj zmienną `result` z wygenerowanymi danymi testowymi
# 2. Użyj funkcji `itertools.permutations()`
# 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 permutations, \
'Variable `result` has invalid type, should be permutations'

>>> result = list(result)

>>> from pprint import pprint
>>> pprint(result, width=72, sort_dicts=False)
[('login', 'edit_profile', 'logout'),
 ('login', 'logout', 'edit_profile'),
 ('edit_profile', 'login', 'logout'),
 ('edit_profile', 'logout', 'login'),
 ('logout', 'login', 'edit_profile'),
 ('logout', 'edit_profile', 'login')]
"""
from itertools import permutations


ACTIONS = ['login', 'edit_profile', 'logout']

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