Python Dictionary Trouble: Grouping by elements in tuple key
Question: so I have a dictionary that looks something like this, with 4 element tuples
as keys, and a list of lists as corresponding values. (yay indexing)

    
    
    {('A002', 'R051', '02-00-00', 'LEXINGTON AVE'): [[datetime.datetime(2015, 6, 20, 0, 0),
                                                      750],
                                                     [datetime.datetime(2015, 6, 21, 0, 0),
                                                      576],
                                                     [datetime.datetime(2015, 6, 22, 0, 0),
                                                      1486],
                                                     [datetime.datetime(2015, 6, 23, 0, 0),
                                                      595],
                                                     [datetime.datetime(2015, 6, 24, 0, 0),
                                                      841],
                                                     [datetime.datetime(2015, 6, 25, 0, 0),
                                                      1072],
                                                     [datetime.datetime(2015, 6, 26, 0, 0),
                                                      1049]],
     ('A002', 'R051', '02-00-01', 'LEXINGTON AVE'): [[datetime.datetime(2015, 6, 20, 0, 0),
                                                      670],
                                                     [datetime.datetime(2015, 6, 21, 0, 0),
                                                      457],
                                                     [datetime.datetime(2015, 6, 22, 0, 0),
                                                      1189],
                                                     [datetime.datetime(2015, 6, 23, 0, 0),
                                                      505],
                                                     [datetime.datetime(2015, 6, 24, 0, 0),
                                                      665],
                                                     [datetime.datetime(2015, 6, 25, 0, 0),
                                                      354],
                                                     [datetime.datetime(2015, 6, 26, 0, 0),
                                                      651]]}
    

I want to modify this dictionary so that I combine values for all keys that
have the same 1st, 2nd, and 4th tuple elements. (as the two keys up there do).
I would like to combine those two key tuples into one key tuple (so that my
combined key is just `('A002', 'R051', 'LEXINGTON AVE')`) and combine the
values as well. Is this possible in python?

So, for instance, the first value would be [datetime.datetime(2015, 6, 20, 0,
0), 1420] ----- which is 670 + 750, in this case

Thanks in advance.


Answer: Yep, just go ahead and make another dictionary. Supposing the data you have
above is stored in `data`, we'll make a dictionary called `short_data`:

    
    
    short_data = {}
    for key, value in data.items():
        short_key = (key[0], key[1], key[3])
        if short_key in short_data:
            short_data[short_key].extend(value)
        else:
            short_data[short_key] = value
    

Or, if you don't mind using a `defaultdict`, you can make this shorter:

    
    
    import collections
    
    short_data = collections.defaultdict(list)
    for key, value in data.items():
        short_key = (key[0], key[1], key[3])
        short_data[short_key].extend(value)
    

If you'd like to combine the values by adding them, I'd suggest using a
`Counter`:

    
    
    import collections
    short_data = collections.defaultdict(collections.Counter)
    for key, value in data.items():
        short_key = (key[0], key[1], key[3])
        short_data[short_key] += collections.Counter(dict(data[key]))
    

