# Why we use dictionary here instead of list?

Hi all! I am not sure if i still understand the code here. So for the first FOR, we just change the format of end_date column. Then 2nd FOR is to calculate the length. But why we create the dictionary instead of list here, i don’t understand when to use the list and when to use the dictionary. I printed the appt_lengths to see the key and the key value.i know we try to see frequency in this loop. However, i dont understand the return at all in this case. I pasted the part of result here. What does shows ‘datetime.timedelta(0,31440):3732’ means? Thank you in advance!!

{datetime.timedelta(0, 31440): 3732, datetime.timedelta(0, 20040): 1, datetime.timedelta(0, 41340): 1115, datetime.timedelta(0, 21120): 1, datetime.timedelta(0, 20760): 1,

``````for row in potus:
end_date = row[3]
end_date = dt.datetime.strptime(end_date, "%m/%d/%y %H:%M")
row[3] = end_date

appt_lengths={}
for row in potus:
appt_start_date=row[2]
appt_end_date=row[3]
length=appt_end_date-appt_start_date
if length not in appt_lengths:
appt_lengths[length]=1
else:
appt_lengths[length]+=1
print(appt_lengths)

min_length=min(appt_lengths)
max_length=max(appt_lengths)
``````

If you use a list to store the frequency of all the lengths, you will have something like this:

`[9, 11, 22, 1, 1,185, 1, 4, 99 . . .]`
But only looking at this you do not know to what length these frequencies refer to. In a dictionary we have it like this;

` {8940: 9, 10740: 11, 12540: 22, 13440: 1, 13740: 1, 14340: 185, 14940: 1, 15240: 4, 16140: 99, . . .}`

In this case, you can see that the frequency 9, for example, refers to appointments with the length equal to 8940 seconds. The information is much more clear and easy to access this way.

The reason why you see `datetime.timedelta(0, 31440)` is that this is a timedelta object. When you calculated the `length` variable, you did a subtraction of dates, which return a timedelta object. Although this is out of scope for this mission, you can extract the actual time difference from this timedelta object and have the data in the dictionary just like the one above. All you need to do is change the `if` statement part of your code for this:

``````if length.seconds in appt_lengths:
appt_lengths[length.seconds] += 1
else:
appt_lengths[length.seconds] = 1
``````

I edited your topic to format the code properly.

Thank you @otavios.s. I changed the if statement as you said. Please see below error shows that line 5 readonly attribute.

appt_lengths={}
for row in potus:
appt_start_date=row[2]
appt_end_date=row[3]
length.seconds=appt_end_date-appt_start_date
if length.seconds in appt_lengths:
appt_lengths[length.seconds] += 1
else:
appt_lengths[length.second] = 1

Go back to my previous questions about datetime.timedelta(0, 31440) so it is subtraction, does it mean 31440 seconds has frequency 3732 ?

{datetime.timedelta(0, 31440): 3732, datetime.timedelta(0, 20040): 1, datetime.timedelta(0, 41340): 1115, datetime.timedelta(0, 21120): 1, datetime.timedelta(0, 20760): 1,

Thank you!

Oh, that was a typo on my side, I’m sorry. Notice that in the last line it is written `second` instead of `seconds`. Just fix that and you should be fine. I’ll fix it on my post.

Yes, exactly.

Please make sure to always format your code properly. It helps people trying to answer your question to understand the code. If you do not know how to do it, please check the Technical Question Guidelines.