Python: from two lists to a dictionary


/ Published in: Python
Save to your folder(s)

create a dictionary from the data in two lists


Copy this code and paste it in your HTML
  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


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.