DEV Community

Super Kai (Kazuya Ito)
Super Kai (Kazuya Ito)

Posted on • Edited on

itertools in Python (6)

Buy Me a Coffee

*Memo:


itertools has the functions to create iterators.

*more-itertools has more functions by installing with pip install more-itertools.


product() can return the iterator which does cartesian product with the elements of *iterables one by one to return a tuple of zero or more elements one by one as shown below:

*Memo:

  • The 1st arguments are *iterables(Optional-Default:()-Type:Iterable):
    • Don't use any keywords like *iterables=, iterables=, etc.
  • The 2nd argument is repeat(Optional-Default:1-Type:int):
    • It's the length of the returned tuple.
    • It must be 0 <= x.
    • repeat= must be used.
from itertools import product

v = product()
v = product((), repeat=1)

print(v)
# <itertools.product object at 0x000001BE99723500>

print(next(v)) # ()
print(next(v)) # StopIteration:
Enter fullscreen mode Exit fullscreen mode
from itertools import product

v = product('ABC')
v = product(['A', 'B', 'C'])

print(next(v)) # ('A',)
print(next(v)) # ('B',)
print(next(v)) # ('C',)
print(next(v)) # StopIteration:
Enter fullscreen mode Exit fullscreen mode
from itertools import product

for x in product('ABC', repeat=2):
# for x in product(['A', 'B', 'C'], repeat=2):
    print(x)
# ('A', 'A')
# ('A', 'B')
# ('A', 'C')
# ('B', 'A')
# ('B', 'B')
# ('B', 'C')
# ('C', 'A')
# ('C', 'B')
# ('C', 'C')
Enter fullscreen mode Exit fullscreen mode
from itertools import product

for x in product('ABC', repeat=3):
# for x in product(['A', 'B', 'C'], repeat=3):
    print(x)
# ('A', 'A', 'A')
# ('A', 'A', 'B')
# ('A', 'A', 'C')
# ('A', 'B', 'A')
# ('A', 'B', 'B')
# ('A', 'B', 'C')
# ('A', 'C', 'A')
# ('A', 'C', 'B')
# ('A', 'C', 'C')
# ('B', 'A', 'A')
# ('B', 'A', 'B')
# ('B', 'A', 'C')
# ('B', 'B', 'A')
# ('B', 'B', 'B')
# ('B', 'B', 'C')
# ('B', 'C', 'A')
# ('B', 'C', 'B')
# ('B', 'C', 'C')
# ('C', 'A', 'A')
# ('C', 'A', 'B')
# ('C', 'A', 'C')
# ('C', 'B', 'A')
# ('C', 'B', 'B')
# ('C', 'B', 'C')
# ('C', 'C', 'A')
# ('C', 'C', 'B')
# ('C', 'C', 'C')
Enter fullscreen mode Exit fullscreen mode
from itertools import product

for x in product('ABC', repeat=4):
# for x in product(['A', 'B', 'C'], repeat=4):
    print(x)
# ('A', 'A', 'A', 'A')
# ('A', 'A', 'A', 'B')
# ('A', 'A', 'A', 'C')
# ('A', 'A', 'B', 'A')
# ('A', 'A', 'B', 'B')
# ('A', 'A', 'B', 'C')
# ('A', 'A', 'C', 'A')
# ('A', 'A', 'C', 'B')
# ('A', 'A', 'C', 'C')
# ('A', 'B', 'A', 'A')
# ('A', 'B', 'A', 'B')
# ('A', 'B', 'A', 'C')
# ('A', 'B', 'B', 'A')
# ('A', 'B', 'B', 'B')
# ('A', 'B', 'B', 'C')
# ('A', 'B', 'C', 'A')
# ('A', 'B', 'C', 'B')
# ('A', 'B', 'C', 'C')
# ('A', 'C', 'A', 'A')
# ('A', 'C', 'A', 'B')
# ('A', 'C', 'A', 'C')
# ('A', 'C', 'B', 'A')
# ('A', 'C', 'B', 'B')
# ('A', 'C', 'B', 'C')
# ('A', 'C', 'C', 'A')
# ('A', 'C', 'C', 'B')
# ('A', 'C', 'C', 'C')
# ('B', 'A', 'A', 'A')
# ('B', 'A', 'A', 'B')
# ('B', 'A', 'A', 'C')
# ...
Enter fullscreen mode Exit fullscreen mode
from itertools import product

for x in product('AB', 'C'):
# for x in product(['A', 'B'], ['C']):
    print(x)
# ('A', 'C')
# ('B', 'C')
Enter fullscreen mode Exit fullscreen mode
from itertools import product

for x in product('AB', 'C', repeat=2):
# for x in product(['A', 'B'], ['C'], repeat=2):
    print(x)
# ('A', 'C', 'A', 'C')
# ('A', 'C', 'B', 'C')
# ('B', 'C', 'A', 'C')
# ('B', 'C', 'B', 'C')
Enter fullscreen mode Exit fullscreen mode
from itertools import product

for x in product('AB', 'C', repeat=3):
# for x in product(['A', 'B'], ['C'], repeat=3):
    print(x)
# ('A', 'C', 'A', 'C', 'A', 'C')
# ('A', 'C', 'A', 'C', 'B', 'C')
# ('A', 'C', 'B', 'C', 'A', 'C')
# ('A', 'C', 'B', 'C', 'B', 'C')
# ('B', 'C', 'A', 'C', 'A', 'C')
# ('B', 'C', 'A', 'C', 'B', 'C')
# ('B', 'C', 'B', 'C', 'A', 'C')
# ('B', 'C', 'B', 'C', 'B', 'C')
Enter fullscreen mode Exit fullscreen mode
from itertools import product

for x in product('AB', 'C', repeat=4):
# for x in product(['A', 'B'], ['C'], repeat=4):
    print(x)
# ('A', 'C', 'A', 'C', 'A', 'C', 'A', 'C')
# ('A', 'C', 'A', 'C', 'A', 'C', 'B', 'C')
# ('A', 'C', 'A', 'C', 'B', 'C', 'A', 'C')
# ('A', 'C', 'A', 'C', 'B', 'C', 'B', 'C')
# ('A', 'C', 'B', 'C', 'A', 'C', 'A', 'C')
# ('A', 'C', 'B', 'C', 'A', 'C', 'B', 'C')
# ('A', 'C', 'B', 'C', 'B', 'C', 'A', 'C')
# ('A', 'C', 'B', 'C', 'B', 'C', 'B', 'C')
# ('B', 'C', 'A', 'C', 'A', 'C', 'A', 'C')
# ('B', 'C', 'A', 'C', 'A', 'C', 'B', 'C')
# ('B', 'C', 'A', 'C', 'B', 'C', 'A', 'C')
# ('B', 'C', 'A', 'C', 'B', 'C', 'B', 'C')
# ('B', 'C', 'B', 'C', 'A', 'C', 'A', 'C')
# ('B', 'C', 'B', 'C', 'A', 'C', 'B', 'C')
# ('B', 'C', 'B', 'C', 'B', 'C', 'A', 'C')
# ('B', 'C', 'B', 'C', 'B', 'C', 'B', 'C')
Enter fullscreen mode Exit fullscreen mode

permutations() can return the iterator which permutates the elements of iterable one by one to return a tuple of zero or more elements one by one as shown below:

*Memo:

  • The 1st argument is iterable(Required-Type:Iterable).
  • The 2nd argument is r(Optional-Default:None-Type:int or NoneType):
    • It's the length of the returned tuple.
    • If it's None or not set, the length of iterable is used.
    • It must be 0 <= x.
from itertools import permutations

v = permutations(iterable='')
v = permutations(iterable='', r=None)
v = permutations(iterable='', r=0)
v = permutations(iterable=[])

print(v)
# <itertools.permutations object at 0x000001BE9908AE30>

print(next(v)) # ()
print(next(v)) # StopIteration:
Enter fullscreen mode Exit fullscreen mode
from itertools import permutations

v = permutations(iterable='A')
v = permutations(iterable='A', r=1)
v = permutations(iterable=['A'])

print(next(v)) # ('A',)
print(next(v)) # StopIteration:
Enter fullscreen mode Exit fullscreen mode
from itertools import permutations

v = permutations(iterable='AB')
v = permutations(iterable='AB', r=2)
v = permutations(iterable=['A', 'B'])

print(next(v)) # ('A', 'B')
print(next(v)) # ('B', 'A')
print(next(v)) # StopIteration:
Enter fullscreen mode Exit fullscreen mode
from itertools import permutations

v = permutations(iterable='ABC')
v = permutations(iterable='ABC', r=3)
v = permutations(iterable=['A', 'B', 'C'])

print(next(v)) # ('A', 'B', 'C')
print(next(v)) # ('A', 'C', 'B')
print(next(v)) # ('B', 'A', 'C')
print(next(v)) # ('B', 'C', 'A')
print(next(v)) # ('C', 'A', 'B')
print(next(v)) # ('C', 'B', 'A')
print(next(v)) # StopIteration:
Enter fullscreen mode Exit fullscreen mode
from itertools import permutations

for x in permutations(iterable='ABCD'):
# for x in permutations(iterable='ABCD', r=4):
# for x in permutations(iterable=['A', 'B', 'C', 'D']):
    print(x)
# ('A', 'B', 'C', 'D')
# ('A', 'B', 'D', 'C')
# ('A', 'C', 'B', 'D')
# ('A', 'C', 'D', 'B')
# ('A', 'D', 'B', 'C')
# ('A', 'D', 'C', 'B')
# ('B', 'A', 'C', 'D')
# ('B', 'A', 'D', 'C')
# ('B', 'C', 'A', 'D')
# ('B', 'C', 'D', 'A')
# ('B', 'D', 'A', 'C')
# ('B', 'D', 'C', 'A')
# ('C', 'A', 'B', 'D')
# ('C', 'A', 'D', 'B')
# ('C', 'B', 'A', 'D')
# ('C', 'B', 'D', 'A')
# ('C', 'D', 'A', 'B')
# ('C', 'D', 'B', 'A')
# ('D', 'A', 'B', 'C')
# ('D', 'A', 'C', 'B')
# ('D', 'B', 'A', 'C')
# ('D', 'B', 'C', 'A')
# ('D', 'C', 'A', 'B')
# ('D', 'C', 'B', 'A')
Enter fullscreen mode Exit fullscreen mode
from itertools import permutations

for x in permutations(iterable='ABCD', r=3):
# for x in permutations(iterable=['A', 'B', 'C', 'D'], r=3):
    print(x)
# ('A', 'B', 'C')
# ('A', 'B', 'D')
# ('A', 'C', 'B')
# ('A', 'C', 'D')
# ('A', 'D', 'B')
# ('A', 'D', 'C')
# ('B', 'A', 'C')
# ('B', 'A', 'D')
# ('B', 'C', 'A')
# ('B', 'C', 'D')
# ('B', 'D', 'A')
# ('B', 'D', 'C')
# ('C', 'A', 'B')
# ('C', 'A', 'D')
# ('C', 'B', 'A')
# ('C', 'B', 'D')
# ('C', 'D', 'A')
# ('C', 'D', 'B')
# ('D', 'A', 'B')
# ('D', 'A', 'C')
# ('D', 'B', 'A')
# ('D', 'B', 'C')
# ('D', 'C', 'A')
# ('D', 'C', 'B')
Enter fullscreen mode Exit fullscreen mode
from itertools import permutations

for x in permutations(iterable='ABCD', r=2):
# for x in permutations(iterable=['A', 'B', 'C', 'D'], r=2):
    print(x)
# ('A', 'B')
# ('A', 'C')
# ('A', 'D')
# ('B', 'A')
# ('B', 'C')
# ('B', 'D')
# ('C', 'A')
# ('C', 'B')
# ('C', 'D')
# ('D', 'A')
# ('D', 'B')
# ('D', 'C')
Enter fullscreen mode Exit fullscreen mode
from itertools import permutations

for x in permutations(iterable='ABCD', r=1):
# for x in permutations(iterable=['A', 'B', 'C', 'D'], r=1):
    print(x)
# ('A',)
# ('B',)
# ('C',)
# ('D',)
Enter fullscreen mode Exit fullscreen mode
from itertools import permutations

for x in permutations(iterable='ABCD', r=0):
# for x in permutations(iterable=['A', 'B', 'C', 'D'], r=0):
    print(x)
# ()
Enter fullscreen mode Exit fullscreen mode
from itertools import permutations

for x in permutations(iterable='ABCD', r=5):
# for x in permutations(iterable=['A', 'B', 'C', 'D'], r=5):
    print(x)
# Nothing
Enter fullscreen mode Exit fullscreen mode

Top comments (0)