TypeError: 'list' object is not callable while trying to access a list

user487257 picture user487257 · Apr 20, 2011 · Viewed 514.4k times · Source

I am trying to run this code where I have a list of lists. I need to add to inner lists, but I get the error

TypeError: 'list' object is not callable.

Can anyone tell me what am I doing wrong here.

def createlists():
    global maxchar
    global minchar
    global worddict
    global wordlists

    for i in range(minchar, maxchar + 1):
        wordlists.insert(i, list())
    #add data to list now
    for words in worddict.keys():
        print words
        print  wordlists(len(words)) # <--- Error here.
        (wordlists(len(words))).append(words)  # <-- Error here too
        print "adding word " + words + " at " + str(wordlists(len(words)))
    print wordlists(5)

Answer

orlp picture orlp · Apr 20, 2011

For accessing the elements of a list you need to use the square brackets ([]) and not the parenthesis (()).

Instead of:

print  wordlists(len(words))

you need to use:

print worldlists[len(words)]

And instead of:

(wordlists(len(words))).append(words)

you need to use:

worldlists[len(words)].append(words)