This tutorial guides you on how to resolve error TypeError: ‘list’ object is not callable in Python programming while executing code that split the given list in to equal size of chunks n.
TypeError: ‘list’ object is not callable
For example, the given input list is as follows.
mylist = ['apple', 'boy', 'cat', 'dog', 'elephant', 'frog', 'goat', 'hen', 'ink', 'jug', 'kite', 'lion']
Below is the function that is required to split the given list in to chunks for size n.
def split_chunks(li,n): for i in range(0, len(li), n): yield li[i:i + n]
The output shows the error TypeError: ‘list’ object is not callable while creating list of chunks that is split using the function defined.
n=3 chunks = list(split_chunks(mylist,n)) --------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-17-451a37ad4340> in <module> ----> 1 chunks = list(split_chunks(mylist,n)) TypeError: 'list' object is not callable
In the above code everything looks fine syntactically. Then what could be the problem ?
In the next section let’s understand what could be the reason for this TypeError. Also, let’s learn how to solve the error.
The reason for the above error is Python looks for a “list” and finds it in the global namespace, but it is not the proper “list” i.e., somewhere I would have assigned an actual list instance to the name list and it shadowed the built-in list. Hence we get this TypeError.
Therefore, to resolve the above error I need to delete the name list from global namespace.
Let’s remove “list” from global namespace by running the following del list command.
Therefore, it is recommended to use Python IDE like PyCharm or anyother IDE that you like, so that it highlights the name shadowing to avoid any such errors.
After removing the name “list” from namespace try running the following code to check the output.
n=3 chunks = list(split_chunks(my_list,n)) print(chunks) [['apple', 'boy', 'cat'], ['dog', 'elephant', 'frog'], ['goat', 'hen', 'ink'], ['jug', 'kite', 'lion']]
Yay! it works. You could see the given list is split in to size of chunks n. Hence the issue is resolved.
Hope this is helpful 🙂
You’ll also like:
- Convert list of lists to flat list – flatten list in Python ?
- Concatenate or join two lists using Python
- Single digit number to double digits string conversion in Python
- String Immutability with Examples in Python
- Python Program to Check Given Number is Odd or Even
- Increase the cell width of the Jupyter Notebook in browser
- Add python3 kernel to jupyter IPython notebook ?
- Programs to Print Patterns – Pyramid, Triangle using Star
- How to change the default theme in Jupyter Notebook ?
- Change the Jupyter Notebook startup folder in Windows & Mac
- To run Jupyter Notebook on Windows from command line
- String Slicing in Python with Examples
- Difference between Python’s list methods append and extend ?
- Python equivalent for && (logical and) to use in if statement
- Split list in to chunks of size n in Python
- How to check if the given list is empty in Python ?
- Remove non-numeric characters from string in Python
- Install Python 3 on Windows 10 machine
- Convert negative to positive number in Python
- Create list of lists in Python
- Extract numbers from a string in python
- How to access index and value in for loop – Python?
- Find index of an element in a list – Python
- Prevent the list that changes unexpected after assignment – Python ?