Working with Dates and Times in Python :6th mission

1)I have a doubt that since month_dt is a list how is it calling the method strftime because i think we require an object to call.
below is the given code.thanks in advance.
visitors_per_month = {}

for row in potus:
month_dt = row[2]
month_str = month_dt.strftime("%B, %Y")
if month_str not in visitors_per_month:
visitors_per_month[month_str] = 1
else:
visitors_per_month[month_str] += 1

Hi @f20180547, welcome to the community!

month_dt wouldn’t be a list. In the code, row is a list from the dataset potus, and so row[2] is then going to be the 3rd element of the list. We happened to convert it into a datetime object on the previous screen. So month_dt is going to be a datetime object, which is why we’re able to use .strftime().

If you want to verify, you can take the first couple of rows and print out the row and month_dt so that you can see what it looks like.

for row in potus[:2]:   # just first 2 rows for verification
    month_dt = row[2]
    print('row:', row)
    print('month_dt:', month_dt)

Output:

row: ['Joshua T. Blanton', '2014-12-18T00:00:00', datetime.datetime(2015, 1, 6, 9, 30), '1/6/15 23:59', '', 'potus', 'west wing', 'JointService Military Honor Guard']
month_dt: 2015-01-06 09:30:00

row: ['Jack T. Gutting', '2014-12-18T00:00:00', datetime.datetime(2015, 1, 6, 9, 30), '1/6/15 23:59', '', 'potus', 'west wing', 'JointService Military Honor Guard']
month_dt: 2015-01-06 09:30:00
1 Like