You're not importing defaultdict. Do either: from collections import defaultdict or import collections d = collections.defaultdict(list)
Here is what the above code is Doing:
1. Create a defaultdict with a default value of list.
2. Iterate over the list of words.
3. For each word, sort the letters in the word.
4. Use the sorted word as a key in the defaultdict.
5. Append the word to the list of words associated with that key.
6. Print the list of words associated with each key.