4
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

3
  • 3
    You have used list as a variable name. rename that and it should work. Commented Apr 24, 2018 at 10:01
  • 1
    This is why it's important to provide a minimal reproducible example. The code you have provided is not complete or verifiable, it works fine: repl.it/repls/FrozenDefinitiveSoftwareagent Commented Apr 24, 2018 at 10:03
  • Google led me to this question when I also faced this error when trying to map(). The problem was I had square brackets around my dictionary: pd.DataFrame({'x': [2, 3]})['x'].map([{2:'c', 3:'d'}]) (correct would be pd.DataFrame({'x': [2, 3]})['x'].map({2:'c', 3:'d'})) Commented Aug 12, 2019 at 14:51

5 Answers 5

9

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
Sign up to request clarification or add additional context in comments.

Comments

3

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]

Comments

0
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

Comments

0

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']

Comments

0
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'}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.