DEV Community

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

Posted on

itertools in Python (9)

Buy Me a Coffee

*Memo:

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=0) 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

Top comments (0)