63

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)
Bhargav Rao
  • 41,091
  • 27
  • 112
  • 129
user487257
  • 961
  • 2
  • 7
  • 16

9 Answers9

85

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)
Bhargav Rao
  • 41,091
  • 27
  • 112
  • 129
orlp
  • 98,226
  • 29
  • 187
  • 285
8

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

jotik
  • 14,982
  • 9
  • 48
  • 106
Ikke
  • 90,705
  • 23
  • 91
  • 118
7

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

print  wordlists[len(words)]
eat_a_lemon
  • 3,178
  • 10
  • 30
  • 46
  • 1
    +1 for identifying that wordlists isn't a function (or some callable object) and hence can't be called with an argument(s), resulting in the given error. For a technical explanation of what makes a Python object callable, see http://stackoverflow.com/questions/111234/what-is-a-callable-in-python. If we want to get really technical about this, we use the square brackets because it is a list data-structure http://docs.python.org/tutorial/datastructures.html#more-on-lists – Aaron Newton May 19 '12 at 08:36
5

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.

MonkeyWrench
  • 356
  • 2
  • 8
3

You are attempting to call wordlists here:

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

Try:

print wordlists[len(words)]
samplebias
  • 34,380
  • 6
  • 95
  • 99
3

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.

Rumple Stiltskin
  • 7,691
  • 1
  • 18
  • 24
0

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.

Vinod Kumar
  • 479
  • 5
  • 12
0

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.

Tanu
  • 11
  • 2
-1
del list

above command worked for me

Lukasz Dynowski
  • 5,919
  • 1
  • 46
  • 77
Raveena
  • 19
  • 1
    If you have assigned a variable list, this might happen. Not in the context of this question, but yeah that helped me. Thanks. – Flyn Sequeira Apr 22 '20 at 02:17