Severus15 Severus15 - 20 days ago 8
Python Question

List object is not callable for a list of integers

I have a problem with Python again. I always get the Error: 'List' object not callable. I looked around in Google and tried every given solution, but its still the same.

I cannot get my code to work. I have a list of integers, and I need to give every element to different variables.

dmy = input('What is your date? Please put in like this: 2.11.2016')
dmy.strip(".")
dmy = [int(x) for x in dmy.split('.')]
list(dmy)
print(dmy)

dd = dmy(0)
mm = dmy(1)
yy = dmy(2)


The first part of the code is working. I get the error, while trying to give the list element to another variable so this dmy (0) does not work. But it is in all the books I have this way?

Someone can give some help?

I use Python 3.5.2

Answer

I see what you are trying to do. An element in the list is obtained by list[index] format. While you are trying to call as list(index) which python is interpreting as function call and hence throwing you error:

TypeError: 'list' object is not callable

Corrected code:

dmy = input('What is your date? Please put in like this: 2.11.2016')
dmy.strip(".")
dmy = [int(x) for x in dmy.split('.')]
list(dmy)
print(dmy)

dd = dmy[0]
mm = dmy[1]
yy = dmy[2]
Comments