title: Permutations without repeating the output in Python
body: <p>I want to get all the possible permutations of two different elements grouped by four without repeating the output but repeating elements (obviously you need to repeat elements to make combinations of 4 with only 2 elements).</p>
  
  <p>So, some things I've tried:</p>
  
  <pre><code>sorted(list(map(lambda x: "".join(x), list(permutations('AB', 4)))))
  </code></pre>
  
  <p>returns an empty list. So I tried this instead:</p>
  
  <pre><code>sorted(list(map(lambda x: "".join(x), list(permutations('AABB', 4)))))
  </code></pre>
  
  <p>And it is closed to the output I expect, but full of redundant items, just a few of them:</p>
  
  <pre><code>['AABB','AABB','AABB','AABB','ABAB','ABAB','ABAB',...
  </code></pre>
  
  <p>What I expected to get is actually:</p>
  
  <pre><code>['AABB','ABAB','ABBA','BAAB','BABA','BBAA']
  </code></pre>
  
  <p>I tried also combinations() and product() instead of permutations() without success, but maybe my arguments weren't fit to the problem.</p>
  
  <p>Any ideas?</p>
  
  <p>Thanks a lot in advance!</p>
  
  <p>PS. As pointed out by Antoine, there's a workaround using a set instead of a list, like:</p>
  
  <pre><code>sorted(list(map(lambda x: "".join(x), set(permutations('AABB', 4)))))
  </code></pre>
  
  <p>That gives the expected output, still it is generating all the repetitions anyway but just happen to be filtered out because sets cannot have repeated element, so it's probably not the most efficient way to do it.</p>