fork download
  1. from collections import OrderedDict, defaultdict
  2.  
  3.  
  4. d = [OrderedDict([
  5. ('id', '1'),
  6. ('date', '20170101')]),
  7. OrderedDict([
  8. ('id', '2'),
  9. ('date', '20170102')]),
  10. OrderedDict([
  11. ('id', '4'),
  12. ('date', '20170102')]) ]
  13.  
  14. dd = defaultdict(int, {'1': 14, '2': 5, '3': 7})
  15. id1 = set([ di['id'] for di in d])
  16. id2 = set( dd.keys() )
  17. final_keys = id1 & id2
  18. to_be_del = []
  19. for di in d:
  20. id = di['id']
  21. if id not in final_keys:
  22. to_be_del.append(di)
  23. continue
  24. q = dd[id]
  25. di['quantity'] = q
  26. for di in to_be_del:
  27. d.remove(di)
  28.  
  29.  
  30. print(d)
  31.  
  32.  
Success #stdin #stdout 0.04s 9432KB
stdin
Standard input is empty
stdout
[OrderedDict([('id', '1'), ('date', '20170101'), ('quantity', 14)]), OrderedDict([('id', '2'), ('date', '20170102'), ('quantity', 5)])]