I would use itertools.groupby , grouping based on the first item in the internal list.
So, first I sorted the list based on the first element, and then based it on the group (if the list is already sorted by this element, then you will not need to sort again, you can directly group).
Example -
new_lst = [] for k,g in itertools.groupby(sorted(lst,key=lambda x:x[0]) , lambda x:x[0]): l = list(g) new_lst.append([k,str(sum([int(x[1]) for x in l])), str(sum([int(x[2]) for x in l]))])
Demo -
>>> import itertools >>> >>> lst = [['20150815171000', '1', '2'], ... ['20150815171000', '2', '3'], ... ['20150815172000', '3', '4'], ... ['20150815172000', '4', '5'], ... ['20150815172000', '5', '6'], ... ['20150815173000', '6', '7']] >>> >>> new_lst = [] >>> for k,g in itertools.groupby(sorted(lst,key=lambda x:x[0]) , lambda x:x[0]): ... l = list(g) ... new_lst.append([k,str(sum([int(x[1]) for x in l])), str(sum([int(x[2]) for x in l]))]) ... >>> new_lst [['20150815171000', '3', '5'], ['20150815172000', '12', '15'], ['20150815173000', '6', '7']]