Python itertools.combinations - Get all the combinations of elements

The itertools.combinations() returns an iterable producing all the combinations of elements.

import itertools

c = itertools.combinations(['a', 'b', 'c'], 2)

print(c)  # <itertools.combinations object at 0x10393a450>
print(type(c))  # <class 'itertools.combinations'>

for i in c:

# ('a', 'b')
# ('a', 'c')
# ('b', 'c')

The second argument is two in the example so each combination has two elements.


Powered by Markdown