DEV Community

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

Posted on

itertools in Python (6)

Buy Me a Coffee

*Memos:


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:

*Memos:

  • The 1st or the later arguments are *iterables(Optional-Type:iterable). *Don't use any keywords like *iterables=, iterables=, *iterable=, iterable=, etc.
  • The 2nd argument is repeat(Optional-Default:1-Type:int): *Memos:
    • 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('ABC', repeat=1)
v = product(['A', 'B', 'C'], repeat=1)

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

v = product('ABC', repeat=2)

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

for x in product('ABC', 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):
    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):
    print(x)
# ('A', 'A', 'A', 'A')
# ('A', 'A', 'A', 'B')
# ('A', 'A', 'A', 'C')
# ('A', 'A', 'B', 'A')
# ('A', 'A', 'B', 'B')
# ...
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:

*Memos:

  • The 1st argument is iterable(Required-Type:iterable).
  • The 2nd argument is r(Optional-Default:None-Type:int): *Memos:
    • 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=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

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

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

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

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

for x in permutations(iterable='ABC', r=4):
    print(x)
# Nothing
Enter fullscreen mode Exit fullscreen mode

Top comments (0)

Some comments may only be visible to logged-in visitors. Sign in to view all comments.