title: In Python, find item in list of dicts, using bisect
body: <p>I have a list of dicts, something like this:</p>
  
  <pre><code>test_data = [
      { 'offset':0, 'data':1500 },
      { 'offset':1270, 'data':120 },
      { 'offset':2117, 'data':30 },
      { 'offset':4055, 'data':30000 },
  ]
  </code></pre>
  
  <p>The dict items are sorted in the list according to the <code>'offset'</code> data. The real data could be much longer.</p>
  
  <p>What I want to do is lookup an item in the list given a particular offset value, which is <strong>not</strong> exactly one of those values, but in that range. So, a binary search is what I want to do.</p>
  
  <p>I am now aware of the Python <a href="http://docs.python.org/library/bisect.html" rel="noreferrer"><code>bisect</code></a> module, which is a ready-made binary search—great, but not directly usable for this case. I'm just wondering what is the easiest way to adapt <a href="http://docs.python.org/library/bisect.html" rel="noreferrer"><code>bisect</code></a> to my needs. Here is what I came up with:</p>
  
  <pre><code>import bisect
  
  class dict_list_index_get_member(object):
      def __init__(self, dict_list, member):
          self.dict_list = dict_list
          self.member = member
      def __getitem__(self, index):
          return self.dict_list[index][self.member]
      def __len__(self):
          return self.dict_list.__len__()
  
  test_data_index_get_offset = dict_list_index_get_member(test_data, 'offset')
  print bisect.bisect(test_data_index_get_offset, 1900)
  </code></pre>
  
  <p>It prints:</p>
  
  <pre><code>2
  </code></pre>
  
  <p>My question is, is this the best way to do what I want, or is there some other simpler, better way?</p>