List.
['Chrome', 'Chromium', 'Google', 'Python']
Result.
{'C': ['Chrome', 'Chromium'], 'G': ['Google'], 'P': ['Python']}
I can make it work like this.
alphabet = dict()
for name in ['Chrome', 'Chromium', 'Google', 'Python']:
character = name[:1].upper()
if not character in alphabet:
alphabet[character] = list()
alphabet[character].append(name)
It is probably a bit faster to pre-populate the dictionary with A-Z, to save the key check on every name, and then afterwards delete keys with empty lists. I'm not sure either is the best solution though.
Is there a pythonic way to do this?