title: Categorize elements of a list in python
body: <p>I want to efficiently categorize the elements of a given list <code>L1</code>. This list can be arbitrary long, so I am looking for an efficient way to do the following.</p>
  
  <p>The list <code>L1</code> contains several elements <code>[e_1,...,e_N]</code> that can be compared with a generic function called <code>areTheSame(e1,e2)</code>. If this function returns <code>True</code>, it means that both elements belong to the same category. </p>
  
  <p>At the end, I want to have another list <code>L2</code>, which in turn contains different lists <code>[LC_1, ..., LC_M]</code>. Each <code>LC</code> list contains all the elements from the same category.</p>