NoobCoder
NoobCoder

Reputation: 675

Error: 'List' object is not callable in map() function

def powerof(num):
    return num**2

number = [1,2,3,4,5,6,7,8]
s = list(map( powerof , number))
print(s)

Error : 'list' object is not callable

Upvotes: 4

Views: 25626

Answers (5)

rajvijen
rajvijen

Reputation: 33

This error occurred because you previously used list object.

Never call list() object, if you ever used list before.

list = [1, 2, 3] # remove this list variable name and use any different one, then it will work.

def powerof(num):
    return num ** 2

number = [1, 2, 3, 4, 5, 6, 7, 8]

s = list(map(powerof, number))
print(s)

Output :- [1, 4, 9, 16, 25, 36, 49, 64]

Upvotes: 3

Raju Puttaswamaiah
Raju Puttaswamaiah

Reputation: 19

def even_or_odd(n):
    if n%2==0:
        return "The number {} is even".format(n)
    else:
        return "The number {} is odd".format(n)

numbers=[1,2,3,4,5,6,7,8]

Map function to iterate the numbers/items. First Map object is created in particular location using lazy loading technique.

map(even_or_odd, numbers)
<map at 0x2315b5fba30>

Memory hasn't been instantiated. To instantiate we need to convert map function to list or set. use set if you already using in-built list() in your code.

set(map(even_or_odd, numbers))  #now memory is instantiated and execute the fn with input

output:

{'The number 1 is odd',
 'The number 2 is even',
 'The number 3 is odd',
 'The number 4 is even',
 'The number 5 is odd',
 'The number 6 is even',
 'The number 7 is odd',
 'The number 8 is even'}

Upvotes: 0

Sandeep Kumar
Sandeep Kumar

Reputation: 1

map() can be used to map same type of value from same type data.

def mapExmple(*string):
    var=""
    for i in string:
        var+=i
    return var

exm_tuple=('T','E','X','T')

result=list(map(mapExmple,exm_tuple))
print(result)

Expected Output:

['T', 'E', 'X', 'T']

Upvotes: 0

Anusha
Anusha

Reputation: 11

list = [1, 2, 3]    # remove this list variable name and use any different one, then it will work.

def powerof(num):
    return num**2

number = [1,2,3,4,5,6,7,8]

s = set(map( powerof , number))
print(s)

instead of list use set

Upvotes: 0

jpp
jpp

Reputation: 164843

You have defined list as a variable earlier in your code.

Do not do this. Call your variable lst or perhaps something more descriptive.

Minimal example to replicate your error:

list = [1, 2, 3]

def powerof(num):
    return num**2

number = [1,2,3,4,5,6,7,8]
s = list(map( powerof , number))
print(s)

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-49-7efe90f8f07a> in <module>()
      5 
      6 number = [1,2,3,4,5,6,7,8]
----> 7 s = list(map( powerof , number))
      8 print(s)

TypeError: 'list' object is not callable

Upvotes: 9

Related Questions