Reputation: 45
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)
Upvotes: 1
Views: 2941
Reputation: 19
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)
Upvotes: 0
Reputation: 335
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()
Upvotes: 0
Reputation: 20450
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)
Upvotes: 1