DEV Community

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

Posted on

itertools in Python (7)

Buy Me a Coffee

*Memos:


itertools has the functions to create iterators.

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


combinations() can return the iterator which uniquely combines 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(Required-Type:int): *Memos:
    • It's the length of the returned tuple.
    • It must be 0 <= x.
from itertools import combinations

v = combinations(iterable='', r=0)
v = combinations(iterable=[], r=0)

print(v)
# <itertools.combinations object at 0x0000026906D95CB0>

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

v = combinations(iterable='ABC', r=1)
v = combinations(iterable=['A', 'B', 'C'], r=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 combinations

v = combinations(iterable='ABC', r=2)

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

v = combinations(iterable='ABC', r=3)

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

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

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

combinations_with_replacement() can return the iterator which non-uniquely combines 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(Required-Type:int): *Memos:
    • It's the length of the returned tuple.
    • It must be 0 <= x.
from itertools import combinations_with_replacement

v = combinations_with_replacement(iterable='', r=0)
v = combinations_with_replacement(iterable=[], r=0)

print(v)
# <itertools.combinations_with_replacement object at 0x0000026906DAAED0>

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

v = combinations_with_replacement(iterable='ABC', r=1)
v = combinations_with_replacement(iterable=['A', 'B', 'C'], r=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 combinations_with_replacement

v = combinations_with_replacement(iterable='ABC', r=2)

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

v = combinations_with_replacement(iterable='ABC', r=3)

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

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

for x in combinations_with_replacement(iterable='ABC', r=4):
    print(x)
# ('A', 'A', 'A', 'A')
# ('A', 'A', 'A', 'B')
# ('A', 'A', 'A', 'C')
# ('A', 'A', 'B', 'B')
# ('A', 'A', 'B', 'C')
# ('A', 'A', 'C', 'C')
# ('A', 'B', 'B', 'B')
# ('A', 'B', 'B', 'C')
# ('A', 'B', 'C', 'C')
# ('A', 'C', 'C', 'C')
# ('B', 'B', 'B', 'B')
# ('B', 'B', 'B', 'C')
# ('B', 'B', 'C', 'C')
# ('B', 'C', 'C', 'C')
# ('C', 'C', 'C', 'C')
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.