join two lists of dictionaries on a single key
Question: Given `n` lists with `m` dictionaries as their elements, I would like to
produce a new list, with a joined set of dictionaries. Each dictionary is
guaranteed to have a key called "index", but could have an arbitrary set of
keys beyond that. The non-index keys will never overlap across lists. For
example, imagine the following two lists:

    
    
    l1 = [{"index":1, "b":2}, {"index":2, "b":3}, {"index":3, "green":"eggs"}]
    l2 = [{"index":1, "c":4}, {"index":2, "c":5}]
    

(`"b"` would never appear in `l2`, since it appeared in `l1`, and similarly,
`"c"` would never appear in `l1`, since it appeared in `l2`)

I would like to produce a joined list:

    
    
    l3 = [{"index":1, "b":2, "c":4}, 
          {"index":2, "b":3, "c":5}, 
          {"index":3, "green":"eggs"}]
    

What is the most efficient way to do this in Python?


Answer: 
    from collections import defaultdict
    
    l1 = [{"index":1, "b":2}, {"index":2, "b":3}, {"index":3, "green":"eggs"}]
    l2 = [{"index":1, "c":4}, {"index":2, "c":5}]
    
    d = defaultdict(dict)
    for l in (l1, l2):
        for elem in l:
            d[elem['index']].update(elem)
    l3 = d.values()
    
    # l3 is now:
    
    [{'b': 2, 'c': 4, 'index': 1},
     {'b': 3, 'c': 5, 'index': 2},
     {'green': 'eggs', 'index': 3}]
    

**EDIT** : Since `l3` is not guaranteed to be sorted (`.values()` returns
items in no specific order), you can do as @user560833 suggests:

    
    
    from operator import itemgetter
    
    ...
    
    l3 = sorted(d.values(), key=itemgetter("index"))
    

