"list indices must be integers, not str' error when using strptime function

1.2k views Asked by At

I have a list of dates, written as strings, called depart_date

depart_date = ['3/1/2012', '3/4/2012', '3/11/2012']

etc.

I'd like to convert them to date objects. This is what I have:

for i in depart_date:
    dep_date = datetime.strptime(depart_date[i], '%m/%d/%Y')

and it gives me the TypeError: list indices must be integers, not str. I'm not sure why I'm getting this error because I thought the strptime function was supposed to convert strings into date objects.

Any help??

1

There are 1 answers

2
maraaaaaaaa On BEST ANSWER

You need to add .Length

for i in depart_date.Length:
    dep_date = datetime.strptime(depart_date[i], '%m/%d/%Y')

Alternative:

for i in depart_date:
    dep_date = datetime.strptime(i, '%m/%d/%Y')