I want to get all possible permutations of two different elements grouped in four, without repeating the output, but repeating elements (obviously, you need to repeat the elements to create combinations of 4 with only two elements).
So, some things I tried:
sorted(list(map(lambda x: "".join(x), list(permutations('AB', 4)))))
returns an empty list. So I tried this:
sorted(list(map(lambda x: "".join(x), list(permutations('AABB', 4)))))
And it is closed to the output that I expect, but filled with redundant elements, just a few of them:
['AABB','AABB','AABB','AABB','ABAB','ABAB','ABAB',...
What I was expecting to get is actually:
['AABB','ABAB','ABBA','BAAB','BABA','BBAA']
I also tried functions () and product () instead of permutations () without success, but maybe my arguments did not match this problem.
Any ideas?
Thank you very much!
PS. As Antoine pointed out, there is a workaround that uses a set instead of a list, for example:
sorted(list(map(lambda x: "".join(x), set(permutations('AABB', 4)))))
This gives the expected result, but it still generates all the repetitions, but it can just be filtered because the sets cannot have a repeating element, so this is probably not the most efficient way to do this.