title: Combining two lists of objects based on an attribute
body: <p>Every example of list or set usage in Python seems to include trivial cases of integers but I have two lists of objects where the name attribute defines whether two objects instances are "the same" or not (other attributes might have different values).</p>
  
  <p>I can create a list that contains all items from both lists, sorted, with </p>
  
  <pre><code>tmpList = sorted(list1 + list2, key=attrgetter('name'))
  </code></pre>
  
  <p>but how do I do the same so that list items that have the same value in the name attribute are selected from the second list?</p>
  
  <p>E.g combining these two lists </p>
  
  <pre><code>list1 = [obj('Harry',18), obj('Mary',27), obj('Tim', 7)]
  list2 = [obj('Harry', 22), obj('Mary', 27), obj('Frank', 40)]
  </code></pre>
  
  <p>would result in</p>
  
  <pre><code>list = [obj('Harry',22), obj('Mary', 27), obj('Tim', 7), obj('Frank', 40)]
  </code></pre>
  
  <p>(I used obj() as a short-hand notation for an object that has two attributes.)</p>
  
  <p>It seems like I can't use the attrgetter() function in most set and list functions like I can with the sorted() function so I can't figure out how I'm supposed to do this. I suspect I could use a lambda function, but I'm not too familiar with functional programming so I don't seem to manage to figure it out.</p>
  
  <p>The naive solution (first pick all items that are unique in both lists and then combine that list with what is left of the second list) seems quite long-winded so there must be a simpler way in Python. </p>