title: Compare several (but not all) elements in a list of namedtuples
body: <p>I have a list of namedtuples that can be quite long (for the moment it can reach 10.000 rows but in the future it could be much more).</p>
  <p>I need to compare several elements of each namedtuple with all other namedtuples from the list. I am looking for an <strong>efficient and generic way</strong> to do so.</p>
  <p>For simplicity, I will make an analogy with cakes, that should make understanding the problem easier.</p>
  <p>Having a list of namedtuples, where each namedtuple is a cake:</p>
  <pre><code>Cake = namedtuple('Cake', 
                         ['cake_id',
                          'ingredient1', 'ingredient2', 'ingredient3',
                          'baking_time', 'cake_price']
                   )
  </code></pre>
  <p>Both <code>cake_price</code> and <code>baking_time</code> are important. If the cakes have the same ingredients, I want to remove from the list the ones that are not relevant. So any cake (with the same ingredients) that is both equal or more expensive and takes same or longer to bake is not relevant (below there is a detailed example).</p>
  <p>What would be the best way to do it?</p>
  <hr />
  <h1>Approach</h1>
  <p>What I have done so far is to sort the list of named_tuples by <code>cake_price</code> and <code>baking_time</code>:</p>
  <pre><code>sorted_cakes = sorted(list_of_cakes, key=lambda c: (c.cake_price, c.baking_time))
  </code></pre>
  <p>And then creating a new list where I am adding all cakes as long as no cake previously added has the same ingredients, is cheaper and faster to bake.</p>
  <pre><code>list_of_good_cakes = []
      for cake in sorted_cakes:
          if interesting_cake(cake, list_of_good_cakes):
              list_of_good_cakes.append(cake)
  
  def interesting_cake(current_cake, list_of_good_cakes):
      is_interesting = True
      if list_of_good_cakes: #first cake to be directly appended
          for included_cake in list_of_good_cakes:
              if (current_cake.ingredient1 == included_cake.ingredient1 and
                  current_cake.ingredient2 == included_cake.ingredient2 and
                  current_cake.ingredient3 == included_cake.ingredient3 and
                  current_cake.baking_time &gt;= included_cake.baking_time):
  
                  if current_cake.cake_price &gt;= included_cake.cake_price:
                      is_interesting = False
  
      return is_interesting
  </code></pre>
  <p>(I know that having a nested loop is far from optimal, but I cannot think of any other way to do it...)</p>
  <hr />
  <h1>Example:</h1>
  <p>Having</p>
  <pre><code>list_of_cakes = [cake_1, cake_2, cake_3, cake_4, cake_5]
  </code></pre>
  <p>where</p>
  <pre><code>cake_1 = Cake('cake_id'=1,
                'ingredient1'='dark chocolate', 
                'ingredient2'='cookies', 
                'ingredient3'='strawberries',
                'baking_time'=60, 'cake_price'=20)
  
  cake_2 = Cake('cake_id'=2,
                'ingredient1'='dark chocolate', 
                'ingredient2'='cookies', 
                'ingredient3'='strawberries',
                'baking_time'=80, 'cake_price'=20)
  
  cake_3 = Cake('cake_id'=3,
                'ingredient1'='white chocolate', 
                'ingredient2'='bananas', 
                'ingredient3'='strawberries',
                'baking_time'=150, 'cake_price'=100)
  
  cake_4 = Cake('cake_id'=4,
                'ingredient1'='dark chocolate', 
                'ingredient2'='cookies', 
                'ingredient3'='strawberries',
                'baking_time'=40, 'cake_price'=30)
  
  cake_5 = Cake('cake_id'=5,
                'ingredient1'='dark chocolate', 
                'ingredient2'='cookies', 
                'ingredient3'='strawberries',
                'baking_time'=10, 'cake_price'=80)
  </code></pre>
  <p>The expected result would be:</p>
  <pre><code>list_of_relevant_cakes = [cake_1, cake_3, cake_4, cake_5]
  </code></pre>
  <ul>
  <li>cake_1 is the cheapest (and the fastest among the same price) --&gt; IN</li>
  <li>cake_2 has the same price as cake1 and takes longer to bake --&gt; OUT</li>
  <li>cake_3 is a different kind of cake --&gt; IN</li>
  <li>cake_4 is more expensive than cake_1, but faster to bake --&gt; IN</li>
  <li>cake_5 is more expensive than cake_1 and cake_4, but even faster to bake --&gt; IN</li>
  </ul>