I have this:
a = [['huhu', 'dow'], ['haha', 'dow'], ['haha', 'pow'], ['haha', 'dow'], ['haha', 'mat'], ['hihi', 'dow'], ['hihi', 'pow']]
and I want
[['huhu', 'dow'], ['haha', 'dow', 'pow', 'dow', 'mat'], ['hihi', 'dow'], ['hihi', 'pow']]
I have this:
a = [['huhu', 'dow'], ['haha', 'dow'], ['haha', 'pow'], ['haha', 'dow'], ['haha', 'mat'], ['hihi', 'dow'], ['hihi', 'pow']]
and I want
[['huhu', 'dow'], ['haha', 'dow', 'pow', 'dow', 'mat'], ['hihi', 'dow'], ['hihi', 'pow']]
I would recommend using a dictionary here, it is much cleaner. In particular I would recommend a collections.defaultdict
:
from collections import defaultdict
dct = defaultdict(list)
for key, *values in a:
dct[key].extend(values)
Output:
defaultdict(list,
{'huhu': ['dow'],
'haha': ['dow', 'pow', 'dow', 'mat'],
'hihi': ['dow', 'pow']})
If you want this as a list, it is a fairly simple comprehension:
[[k, *v] for k, v in dct.items()]
# [['huhu', 'dow'], ['haha', 'dow', 'pow', 'dow', 'mat'], ['hihi', 'dow', 'pow']]