List.
['Chrome', 'Chromium', 'Google', 'Python']
Result.
{'C': ['Chrome', 'Chromium'], 'G': ['Google'], 'P': ['Python']}
I can make it work as follows.
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βs probably a little faster to pre-populate the AZ dictionary to save a key check for each name, and then delete keys with empty lists. I am not sure if this is the best solution.
Is there a pythonic way to do this?
user479870
source
share