Reputation: 145
Suppose a list L=[a,b]
, I want to find all possible combinations of this list and stop when the combination reach a specific length. For example: I want to stop it after combination reaches length 2
. the result should be ['aa', 'ab', 'ba', 'bb']
Upvotes: 1
Views: 66
Reputation: 4315
Ex.
import itertools
a = ['a','b']
combination = [''.join(x) for x in itertools.product(a,repeat=2)]
print(combination)
O/P:
['aa', 'ab', 'ba', 'bb']
Upvotes: 1