Merge lists in to dictionary based on key in Python
Mon 01 February 2010i've been learning bits of python here and there for work and for play
and I came across a simple solution to a simple problem I had to solve
in python.
There are some things in python that once you realize how things work
you think "that is so simple!". I had one of those moments.
This particular solution came up when the need to compare user disk quotas arose on different filers.
list_a = { 'a' : 123, 'b' : 456 }
list_b = { 'a' : 321, 'b' : 654 }
list_c = {}
for key, val in list_a.items():
for que, wal in list_b.items():
if key == que:
list_c[key] = [ val, wal ]
print list_c
{'a': [123, 321], 'b': [456, 654]}