I'm trying to generate lists of possible permutations of two values, which only contain equal numbers of each value.
The closest I've come is product permutation and then a Counter afterwards, but this is obviously the wrong way around and consumes vast amounts of memory for values over 20.
lst = ['d', 'r']
correct = 0
for x in product(lst, repeat=20):
y = Counter(x)
if y['d'] == y['r']:
correct += 1
print(correct)
I've also tried just a set of permutations but that's even less efficient than the product algorithm for some reason:
lst = ['d', 'r'] * 10
print(len(set(permutations(lst)))
Can anyone suggest a different tool, or a way of restricting product's generation to only the iterations with equal numbers of each element?