title: Accessing list of Python objects by object attribute
body: <p>I don't know if what I am trying to do is so un-Pythonic that I'm simply trying to do it wrong, or if I don't know how to ask the question properly.  It makes sense to me to be able to do this, but I have searched 15 different ways and can't find the answer.</p>
  
  <p>What I want to do seems so simple: I have a list of objects.  I want to access that list by a property of the objects.  This code works:</p>
  
  <pre><code>class Fruit:
      def __init__(self, name, color):
          self.name = name
          self.color = color
  
      def __str__(self):
          return self.name    
  
  class BaseballPlayer:
      def __init__(self, name, number, position):
          self.name = name
          self.number = number
          self.position = position
  
      def __str__(self):
          return self.name    
  
  class ListByProperty(list):
      def __init__(self, property, list):
          super(ListByProperty, self).__init__(list)
          self.property = property
  
      def __getitem__(self, key):
          return [item for item in self if getattr(item, self.property) == key][0]
  
  fruits = ListByProperty('name', [Fruit('apple', 'red'), Fruit('banana', 'yellow')])
  
  baseballTeam = ListByProperty('position', [BaseballPlayer('Greg Maddux', 31, 'P'),
                                             BaseballPlayer('Javy Lopez', 8, 'C')])
  teamByNumber = ListByProperty('number', baseballTeam)
  
  print 'Apples are', fruits['apple'].color
  
  pitcher = baseballTeam['P']
  print 'The pitcher is #%s, %s' % (pitcher.number, pitcher)
  print '#8 is', teamByNumber[8]
  
  &gt;&gt;&gt; Apples are red
  The pitcher is #31, Greg Maddux
  #8 is Javy Lopez
  </code></pre>
  
  <p>But do I really have to make my own list class to do something this simple?  Is there no generic way other than looping or a listcomp?  This seems like it should be a very common thing to do, to have a list of objects and access items in the list by a property of the objects.  It seems like it should be commonly supported in a way similar to <code>sorted(key=...)</code>.</p>
  
  <p>Note that this is not the same case as needing a dict.  In fact, the whole point of using a list of objects instead of a dict is to avoid having to do something like:</p>
  
  <pre><code>fruits = {'apple': Fruit('apple', 'red')}
  </code></pre>
  
  <p>...which requires you to type <code>apple</code> twice.  It seems like there should be a generic way to do something like this:</p>
  
  <pre><code>print 'Apples are', fruits['apple'].color
  </code></pre>
  
  <p>...without having to subclass <code>list</code>.</p>
  
  <p>And okay, you can build a dict like this:</p>
  
  <pre><code>fruits = [Fruit('apple', 'red'), Fruit('banana', 'yellow')]
  fruits = {f.name: f for f in fruits}
  </code></pre>
  
  <p>Or you can one-line it, but that still seems...uh...syntactically sour?  :)</p>
  
  <p>The best way I've figured out so far is:</p>
  
  <pre><code>class DictByProperty(dict):
      def __init__(self, property, list):
          super(DictByProperty, self).__init__({getattr(i, property): i for i in list})
          self.property = property
  
  fruits = DictByProperty('name', [Fruit('apple', 'red')])
  </code></pre>
  
  <p>Oh well, thanks, I've learned a lot already from this question.</p>