I am a new python user, and I need help about combining list elements under a condition.
I have a list like this:
x = [['a', 10, 20], ['b', 10, 20], ['a', 20, 100]]
I would like to combine list elements which start with the same letter in a list by summing up the other elements. for example, I’d like to obtain this list for x:
x = [['a', 30, 120], ['b', 10, 20]]
How can I achieve this ?
A one-liner using itertools.groupby():
A simple solution:
Without sorting the list using
defaultdict():