5

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?

6
  • you must append to words's values not itself. like words[key].append()
    – Mazdak
    Jan 6, 2015 at 1:51
  • You can't use words.append(), since it doesn't exist. The correct code depends on what you actually want to do.
    – recursive
    Jan 6, 2015 at 1:51
  • 1
    YOu need some key, words[key].append(word)
    – Marcin
    Jan 6, 2015 at 1:51
  • still getting an error :/ Jan 6, 2015 at 1:54
  • @Freddy-FazBear, which error are you getting?! Edit your Q to show the traceback. By saying you're getting an error and not saying anything about which one and where it looks like you're trying to make it impossible for us to help you...!-) Jan 6, 2015 at 1:57

3 Answers 3

4

collections.defaultdict is a type of dictionary; it does not have an append method. That is only available on the list type.

Looking at your code, there is not really a reason for you to be using a dictionary-like structure. Your goal is to collect individual items, not key/value pairs. words should just be a list:

words = []
with open('/usr/share/dict/words') as fin:
    for word in fin:
        if len(word) == 5: 
            words.append(word)

Also, you probably want to call str.rstrip() on each line in order to remove the trailing newline:

for word in fin:
    word = word.rstrip()
0
4

For clarification, your issue is that you're not setting a key for your .append()

When you instantiate your defaultdict(list) object, what you're doing is telling that dictionary-like object that each VALUE for a given KEY will be a list.

See here for a more defined example / explanation.

-1

A defaultdict is just like a regular dictionary, except that, when you lookup the value of a key that isn't in the dictionary, it returns a default value instead of raising a KeyError.

It looks like you're using a code snippet found here. The original intent of that snippet was to group words by their first letter. Represented as a dictionary, the letters of the alphabet would be the keys, and lists of words would be the values. You want to append each word to the entry in the dictionary corresponding to the proper start letter.

If you're trying to achieve the same objective as that asker was, and you want to use a defaultdict for it, one way of proceeding would be to replace words.append(word) with words[word[0]].append(word), or something to that effect.

If you're not trying to do the same, iCodez's answer is the way to go.

1
  • I never knew about the key part or what it does until now, and also sorry for no including the whole traceback error. This works also, thanks for the help and explanation. ;) Jan 6, 2015 at 2:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.