title: Partitioning non-unique entities into unique sets with Python
body: <p>I have a list of items, e.g. <code>[1,1,1,1,2,2]</code>, and I am trying to find all the unique groups where these items are bundled into tuples of length one or two. For example, for the group above, I would like to find the following 10 possible groupings:</p>
  
  <pre><code>[[(1,),(1,),(1,),(1,),(2,),(2,)],
   [(1,1),(1,),(1,),(2,),(2,)],
   [(1,2),(1,),(1,),(1,),(2,)],
   [(2,2),(1,),(1,),(1,),(1,)],
   [(1,1),(1,1),(2,),(2,)],
   [(1,1),(1,2),(1,),(2,)],
   [(1,2),(1,2),(1,),(1,)],
   [(2,2),(1,1),(1,),(1,)],
   [(1,1),(1,1),(2,2)],
   [(1,1),(1,2),(1,2)]]
  </code></pre>
  
  <p>I've been playing with itertools to but can only manage to use it to find unique possible tuples (e.g. <code>set(list(itertools.combinations((1,1,1,1,2,2),2)))</code>) and any searches I do pop up solutions where the size of each group is constant and/or duplication of elements is not considered (<a href="https://stackoverflow.com/questions/464864/python-code-to-pick-out-all-possible-combinations-from-a-list">example1</a>, <a href="https://stackoverflow.com/questions/10568081/partitioning-a-sequence-into-sets-of-unique-pairs/10570837#10570837">example2</a>).</p>
  
  <p>Ultimately, I am looking for a solution that will work for cases that are all ones (<code>[1,1,1,...,1]</code>), all twos (<code>[2,2,2,...,2]</code>) or some intermediate combination that includes an arbitrary number of 1s and 2s.</p>