Shahid Khan
Shahid Khan

Reputation: 145

finding all possible arrangements of list elements until the combination reaches a provided length

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

Answers (1)

bharatk
bharatk

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

Related Questions