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

PythonCallable

Python Problem Overview


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)

Python Solutions


Solution 1 - Python

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)

Solution 2 - Python

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

Solution 3 - Python

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

print  wordlists[len(words)]

Solution 4 - Python

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.

Solution 5 - Python

You are attempting to call wordlists here:

print  wordlists(len(words)) <--- Error here.

Try:

print wordlists[len(words)]

Solution 6 - Python

Try wordlists[len(words)]. () is a function call. When you do wordlists(..), python thinks that you are calling a function called wordlists which turns out to be a list. Hence the error.

Solution 7 - Python

Check your file name in which you have saved your program. If the file name is wordlists then you will get an error. Your filename should not be same as any of methods{functions} that you use in your program.

Solution 8 - Python

del list

above command worked for me

Solution 9 - Python

Even I got the same error, but I solved it, I had used many list in my work so I just restarted my kernel (meaning if you are using a notebook such as Jupyter or Google Colab you can just restart and again run all the cells, by doing this your problem will be solved and the error vanishes.

Thank you.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser487257View Question on Stackoverflow
Solution 1 - PythonorlpView Answer on Stackoverflow
Solution 2 - PythonIkkeView Answer on Stackoverflow
Solution 3 - Pythoneat_a_lemonView Answer on Stackoverflow
Solution 4 - PythonMunkeyWrenchView Answer on Stackoverflow
Solution 5 - PythonsamplebiasView Answer on Stackoverflow
Solution 6 - PythonRumple StiltskinView Answer on Stackoverflow
Solution 7 - PythonVinod KumarView Answer on Stackoverflow
Solution 8 - PythonRaveenaView Answer on Stackoverflow
Solution 9 - PythonTanuView Answer on Stackoverflow