I encountered a problem with a code snippet that I'm trying to use. Here is the code:
from collections import defaultdict
from string import ascii_lowercase
words = defaultdict(list)
with open('/usr/share/dict/words') as fin:
for word in fin:
if len(word) == 5:
words.append(word)
Whenever I try to run the code it returns the error:
AttributeError: type object 'collections.defaultdict' has no attribute 'append'
Any suggestions?
words
's values not itself. likewords[key].append()
words.append()
, since it doesn't exist. The correct code depends on what you actually want to do.words[key].append(word)