Python update dictionary based on key value pair
Question: this question is how to check two dictionary list and for same keys and update
main dictionary with key value pair from other dictionary.

# First dictionary

    
    
    main = [
    {'country': u'CYPRUS', 'naziv': 'AKEL', 'FCI': 2}, 
    {'country': u'CYPRUS', 'naziv': 'DIKO', 'FCI': 4}, 
    ]
    

# Second

    
    
    second = [
    {'likes': '8625.00', 'talks': '1215.00', 'naziv': 'AKEL'}, 
    {'likes': '2746.00', 'talks': 0, 'naziv': 'DIKO'}, 
    ]
    

# Final output - example

    
    
    output = [
        {'country': u'CYPRUS', 'naziv': 'AKEL', 'FCI': 2,'likes': '8625.00', 'talks': '1215.00'}
        {'country': u'CYPRUS', 'naziv': 'DIKO', 'FCI': 4,'likes': '2746.00', 'talks': 0}, 
    ]
    

Is there any fast way to do this. I was trying with update one dictionary but
dont works.

    
    
    for dt, k in itertools.groupby(sorted(second, key=itemgetter('naziv')), key=itemgetter('naziv')):
        maindict = {'naziv': dt}
        for d in k:
            maindict.update(d)
        main.append(maindict)
    

Python 2.7


Answer: Another one may be -

    
    
    import itertools
    main = [
    {'country': u'CYPRUS', 'naziv': 'AKEL', 'FCI': 2}, 
    {'country': u'CYPRUS', 'naziv': 'DIKO', 'FCI': 4}, 
    ]
    second = [
    {'likes': '8625.00', 'talks': '1215.00', 'naziv': 'AKEL'}, 
    {'likes': '2746.00', 'talks': 0, 'naziv': 'DIKO'}, 
    ]
    
    
    lst = sorted(itertools.chain(main,second), key=lambda x:x['naziv'])
    list_c = []
    for k,v in itertools.groupby(lst, key=lambda x:x['naziv']):
        d = {}
        for dct in v:
            d.update(dct)
        list_c.append(d)
    print list_c
    

"Shemeless copy of mgilson"

