How can I use python itertools.groupby() to group a list of strings by their first character?
You might want to create dict
afterwards:
from itertools import groupby
d = {k: list(v) for k, v in groupby(sorted(tags), key=lambda x: x[0])}
groupby(sorted(tags), key=operator.itemgetter(0))
>>> for i, j in itertools.groupby(tags, key=lambda x: x[0]):
print(i, list(j))
a ['apples', 'apricots']
o ['oranges']
p ['pears', 'peaches']