Python

Typeerror: ‘list’ Object is Not Callable [Solved]

While working in python language, you must have inserted and accessed elements from a list or dictionary several times. We have mainly used the index of that particular element to access it. We must have used the square brackets around the index number to fetch the elements. Whenever a user tries to fetch the list element by using any other brackets, the type error occurs saying: ‘list’ object is not callable. This guide will show how this error occurs and how it could be resolved with a little change using some examples. So, we have been using the Spyder3 python tool to illustrate our examples. Thus, let’s start looking at them.

Example 01:

We have started our first example code by declaring a string-type list having 5 string values in it. The list name is “L”. The “for” loop has been used here to iterate the elements of list “L”. The loop will continue to iterate up to the length of a list. While the iterator index “n” is in the range of a list length, it will continue to call the built-in method upper() to convert the value at index “n” of a list to the upper case. This upper case value would be again saved into the index “n” of a list “L”.

You can see, we have used the simple brackets to specify the index number that is being used to call the upper() method here. After that, a print clause is utilized to output the upper case value inserted in the index “n” of a list “L”. There is the incorrect syntax for specifying the index, i.e., simple brackets “()”. After the “for” loop, the whole updated list will be printed.

  • L = [ “Linux’, “Debian’, “Oracle’. “LTS”, “Iphone’]
  • for n in range(len(L)):
    • L[n] = L(n).upper()
    • print(L(n))
  • print(L)

Use the “Run” button held at the menu bar of the python tool, i.e., Spyder3, to debug and run this newly created program code. After running this code, we have got the TypeError stating that the “list” object is not callable.

To resolve the TypeError, replace the simple brackets with the square brackets where the index “n” has been defined in the list “L” on line numbers 4 and 5.

  • L = [ “Linux’, “Debian’, “Oracle’. “LTS”, “Iphone’]
  • for n in range(len(L)):
    • L[n] = L[n].upper()
    • print(L[n])
  • print(L)

When we run this updated code once again, it displays the upper case string values of the list one by one separately due to the for loop. In the end, the whole uppercase list has been displayed.

Example 02:

We have been taking another example to create and solve the TypeError. So, we have initialized the list “L” with some strings in it. A variable “i” has been declared and initialized to 0. Another string type variable, “s1” has been declared and initialized as empty.

The “while” loop has been used here to check if the value of the variable “I” is less than the length of a list “L”. If so, then the variable s1 will be concatenated with the space and the value at index “I”, i.e., the same as variable “I”. This updated value will be saved into variable “s1” once again, and the iterator variable “I” will be incremented by 1. The s1 variable is printed out.

  • L = [‘Hi’, ‘My’, ‘name’, ‘is’, ‘Linux’]
  • i = 0
  • s1 = ”
  • while i < len(L):
  • s1 = s1 + ‘ ‘ + L(i)
  • i += 1
  • print(s1)

When we run this code, it throws a TypeError exception at line 5.

So, we have replaced the simple brackets with square brackets at line 5.

  • L = [‘Hi’, ‘My’, ‘name’, ‘is’, ‘Linux’]
  • i = 0
  • s1 = ”
  • while i < len(L):
  • s1 = s1 + ‘ ‘ + L[i]
  • i += 1
  • print(s1)

After running this code again, we have got the resultant value of the s1 variable. You can see, it shows the sentence created by a while loop.

Conclusion:

This article contains simple and easy illustrations to solve the Typeerror caused by some syntax issue showing that the object “list” is not callable. At the start of this article, we have stated some basics required to understand the whole article. We hope it will be beneficial for you.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content