1
AttributeError: 'list' object has no attribute 'time'    

SO i want this list of list timeList = [ [ ],[ ],[ ] ] to store my firstTime, secondtime and thirdTime but it throws me an error. any idea on this

from datetime import datetime, date,timedelta

timeList = [[],[],[]]
timeList[1].append(datetime.now())
secondTime=timeList[1].time() # throws an error 
print(secondTime)

3 Answers 3

1

You want this:

>>> secondTime = timeList[1][0].time() 
>>>
>>> timeList
[[], [datetime.datetime(2020, 10, 2, 17, 38, 12, 274423)], []]
>>> 
>>> timeList[1]
[datetime.datetime(2020, 10, 2, 17, 38, 12, 274423)]
>>> 
>>> secondTime
datetime.time(17, 38, 12, 274423)
Sign up to request clarification or add additional context in comments.

Comments

0

Your problem is that timeList is a list of lists, hence every item in it is from type list.

When your run:

secondTime=timeList[1].time()

You are accessing the second item of timeList, which is a list itself, and trying to call time.

Try the following line instead:

secondTime=timeList[1][0].time()

Comments

0

you need to give the location of the item by saying where in the list you want to find it, and then look inside that sub-list. this works:

from datetime import datetime, date,timedelta
timeList = [[],[],[]]
timeList[1].append(datetime.now())
secondTime=timeList[1][0].time() # throws an error 
print(secondTime)

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.