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
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
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]
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'}
listas a variable name. rename that and it should work.pd.DataFrame({'x': [2, 3]})['x'].map([{2:'c', 3:'d'}])(correct would bepd.DataFrame({'x': [2, 3]})['x'].map({2:'c', 3:'d'}))