DEV Community

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

Posted on • Edited on

itertools in Python (7)

Buy Me a Coffee

*Memo:


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:

*Memo:

  • The 1st argument is iterable(Required-Type:Iterable).
  • The 2nd argument is r(Required-Type:int):
    • 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='ABCD', r=1)
v = combinations(iterable=['A', 'B', 'C', 'D'], r=1)

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

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

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

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

for x in combinations(iterable='ABCD', r=5):
# for x in combinations(iterable=['A', 'B', 'C', 'D'], r=5):
    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:

*Memo:

  • The 1st argument is iterable(Required-Type:Iterable).
  • The 2nd argument is r(Required-Type:int):
    • 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='ABCD', r=1)
v = combinations_with_replacement(iterable=['A', 'B', 'C', 'D'], r=1)

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

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

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

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

Top comments (0)