I have this line of code that organizes the words in list into dictionary by the first letter:
for a in list: dictionary[a[0]].append(a)
The problem is, this requires the dictionary keys to each individually have empty list values. I could do this by adding a few extra lines of code previously, but I want to know if there's a way to have the previous line of code deal with this.
Here's the pseudo-code:
- If the key exists, insert a into
dictionary[a[0]]
. - If it does not, do it after creating the key with an empty list.
Is there a way I can do this in a single line of code?