Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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)
like image 608
user487257 Avatar asked Apr 20 '11 19:04

user487257


People also ask

How do I fix TypeError list object is not callable?

The Python "TypeError: 'list' object is not callable" occurs when we try to call a list as a function using parenthesis () . To solve the error, make sure to use square brackets when accessing a list at a specific index, e.g. my_list[0] .

Why is a list not callable?

The most common scenario where Python throws TypeError: 'list' object is not callable is when you have assigned a variable name as “list” or if you are trying to index the elements of the list using parenthesis instead of square brackets.

How do I fix type error int object is not callable?

How to resolve typeerror: 'int' object is not callable. To resolve this error, you need to change the name of the variable whose name is similar to the in-built function int() used in the code.

Why is my object not callable Python?

The Python "TypeError: 'int' object is not callable" occurs when we try to call an integer value as a function. To solve the error, correct the assignment, make sure not to override the built-in int() function, and resolve any clashes between function and variable names.


4 Answers

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) 
like image 163
orlp Avatar answered Sep 21 '22 06:09

orlp


To get elements of a list you have to use list[i] instead of list(i).

like image 23
Ikke Avatar answered Sep 20 '22 06:09

Ikke


wordlists is not a function, it is a list. You need the bracket subscript

print  wordlists[len(words)]
like image 40
eat_a_lemon Avatar answered Sep 20 '22 06:09

eat_a_lemon


I also got the error when I called a function that had the same name as another variable that was classified as a list.

Once I sorted out the naming the error was resolved.

like image 37
MunkeyWrench Avatar answered Sep 22 '22 06:09

MunkeyWrench