Search code examples
pythonlistcallable

Python error: 'list' object is not callable after converting the map function to list


The following codes showed I tried to map a function list and got a type error of "'list' object is not callable".

The L1 is of type 'map', so I used list function to convert it and it still returned an error.

Do you have any idea about this problem? Thanks!

import math
func_list=[math.sin, math.cos, math.exp]
result=lambda L: map(func_list, L)
L=[0,0,0]
L1=result(L)
for x in L1:
    print(x)

the type of result is <class 'function'> the type of result is <class 'map'>

Traceback (most recent call last) 
<ipython-input-22-17579bed9240> in <module>
          6 print("the type of result is " + str(type(result)))
          7 print("the type of result is " + str(type(L1)))
    ----> 8 for x in L1:
          9     print(x)
    
    TypeError: 'list' object is not callable

Solution

  • Please read the documentation for the map(function, iterable) function:

    https://docs.python.org/3/library/functions.html#map

    But you pass list to the function parameter.

    So your example can be replaced with the next code e.g.:

    import math
    
    func_list = [math.sin, math.cos, math.exp]
    
    result=lambda L: map(lambda x: map(lambda func: func(x), func_list), L)
    
    L = [0, 0, 0]
    L1 = result(L)
    
    for x in L1:
        for value in x:
            print(value, end=' ')
        
        print()