How would I merged nested dictionaries in a list in python?
Question: for example if i had the result

    
    
    [{'Germany': {"Luge - Men's Singles": 'Gold'}}, 
    {'Germany': {"Luge - Men's Singles": 'Silver'}},
    {'Italy': {"Luge - Men's Singles": 'Bronze'}}]
    [{'Germany': {"Luge - Women's Singles": 'Gold'}},
    {'Austria': {"Luge - Women's Singles": 'Silver'}},
    {'Germany': {"Luge - Women's Singles": 'Bronze'}}]
    [{'Austria': {'Luge - Doubles': 'Gold'}}, 
    {'Latvia': {'Luge - Doubles': 'Silver'}},
    {'Germany': {'Luge - Doubles': 'Bronze'}}]
    

how would I sort this so that all of the events germany and so on had won
could be under one single title. i.e germany would be germany:Luge - Men's
Singles: Gold, Silver, Luge - Women's Singles: Gold, Bronze, Luge - Doubles:
Bronze.

thanks for any help

EDIT: this is a straight copy and paste from the python shell now to help
confusion:

[{'Germany': {"Luge - Men's Singles": 'Gold'}}, {'Germany': {"Luge - Men's
Singles": 'Silver'}}, {'Italy': {"Luge - Men's Singles": 'Bronze'}}]
[{'Germany': {"Luge - Women's Singles": 'Gold'}}, {'Austria': {"Luge - Women's
Singles": 'Silver'}}, {'Germany': {"Luge - Women's Singles": 'Bronze'}}]
[{'Austria': {'Luge - Doubles': 'Gold'}}, {'Latvia': {'Luge - Doubles':
'Silver'}}, {'Germany': {'Luge - Doubles': 'Bronze'}}]

sorry about that im new to this site. It loops round 3 time one for each
different event and i was wondering if i could get the desired from merging it
after the final loop?


Answer: 
    import collections
    
    merged_result = collections.defaultdict(list)
    
    for L in listoflistsofdicts:
      for d in L:
        for k in d:
          merged_result[k].append(d[k])
    

or if you just have a list of dicts instead of a list of lists of dicts (hard
to say from your Q!-), then just the

    
    
      for d in listofdicts:
        for k in d:
          merged_result[k].append(d[k])
    

part of the loop.

If you want strings rather than lists as the values of `merged_result` then
after the above code add

    
    
    for k in merged_result:
        merged_result[k] = ', '.join(merged_result[k])
    

or, equivalently (but building a new plain dict instead of the defaultdict):

    
    
    merged_result = dict((k, ', '.join(v)) for k, v in merged_result.iteritems())
    

(this assumes Python 2.* -- in Python 3, use `.items` instead of
`.iteritems`).

**Edit** : looking at the sample code it looks like it's invalid syntax for a
list of list of dicts (missing commas) so I showed how to deal with that, too.

