Posted By

magicrebirth on 12/22/09


Tagged

list python dictionary


Versions (?)

Python: from two lists to a dictionary


 / Published in: Python
 

create a dictionary from the data in two lists

  1. # [PSST] Copied for good measure
  2. ListA = ['10', '10', '20', '20', '20', '24']
  3. ListB = ['23', '44', '11', '19', '57', '3']
  4.  
  5. d = {}
  6. for a, b in map(None, ListA, ListB):
  7. if not d.has_key(a):
  8. d[a] = [b]
  9. else:
  10. d[a].append(b)
  11. print d
  12.  
  13.  
  14. # result: {'24': ['3'], '10': ['23', '44'], '20': ['11', '19', '57']}

Report this snippet  

You need to login to post a comment.