title: return dictionaries in list where dictionary key equals value
body: <p>I have a database table with thousands of near duplicates that I would like to merge. I use <code>psycopg2.extras</code> to execute a query and eventually get the results in a list of dictionaries.</p>
  
  <pre><code>dict_cur = conn.cursor(cursor_factory=psycopg2.extras.RealDictCursor)
  query = """SELECT DISTINCT pat_uid, pat_first_name, pat_last_name, pat_dob_dt, pat_chi_no,
         pat_addr, wtg_uid, wtg_pat_uid, std_uid, std_pat_uid, work_uid, work_pat_uid
         from ((patients left outer join waiting 
         on (pat_uid = wtg_pat_uid)) left outer join studies 
         on (std_pat_uid = pat_uid) left outer join worklist
         on (work_pat_uid = pat_uid)) 
         where (pat_first_name, pat_last_name, pat_chi_no)
         in (SELECT pat_first_name, pat_last_name, pat_chi_no from patients 
         group by pat_first_name, pat_last_name, pat_chi_no
         HAVING count(*) &gt;= 2) AND
         pat_chi_no ~ '^[0-9]{10,10}\$'
         order by pat_last_name asc, pat_first_name asc,
         pat_dob_dt asc"""
  
  # Execute query - find all duplicate patients
  dict_cur.execute(query)
  # python list containing complete query results
  res = dict_cur.fetchall()
  </code></pre>
  
  <p>I then find unique id numbers of each patient using a set</p>
  
  <pre><code> chi_set = set()
   for n in res:
          chi_set.add(n['pat_chi_no'])
  </code></pre>
  
  <p>I then plan to loop through each unique <code>pat_chi_no</code> and return the dictionaries which match each <code>pat_chi_no</code>. How do I do this efficiently? I have tried the following edited from <a href="https://stackoverflow.com/questions/15784590/how-can-you-print-a-key-given-a-value-in-a-dictionary-for-python">How can you print a key given a value in a dictionary for Python?</a></p>
  
  <pre><code>def build_dict(seq, key):
      return dict((d[key], dict(d, index=index)) for (index, d) in enumerate(seq))
  
  info_by_chi = build_dict(res, key="pat_chi_no")
  info_by_chi["1111111111"]
  </code></pre>
  
  <p>This only returns one dictionary I know there should be 4 entries in this case.</p>
  
  <p>Sample output would be a subset of the complete list of dictionaries i.e. the list would contain only those dictionaries where <code>lst['pat_chi_no'] == 1111111111</code></p>
  
  <p>eg</p>
  
  <pre><code>{'index': 4218,
   'pat_addr': '10 DOWNING STREET',
   'pat_chi_no': '1111111111',
   'pat_dob_dt': datetime.date(1937, 9, 24),
   'pat_first_name': 'JOHN',
   'pat_last_name': 'DOE',
   'pat_uid': 35359,
   'std_pat_uid': 35359,
   'std_uid': 37138,
   'work_pat_uid': 35359,
   'work_uid': 366,
   'wtg_pat_uid': 35359,
   'wtg_uid': 11307}
  
  
  {'index': 122,
       'pat_addr': '10 DOWNING STREET',
       'pat_chi_no': '1111111111',
       'pat_dob_dt': datetime.date(1937, 9, 24),
       'pat_first_name': 'JOHN',
       'pat_last_name': 'DOE',
       'pat_uid': 1233,
       'std_pat_uid': 3455,
       'std_uid': 2323,
       'work_pat_uid': 2455,
       'work_uid': 366,
       'wtg_pat_uid': 3455,
       'wtg_uid': 1127}
  </code></pre>