Command Line Calendar -- why use calendar.keys()?

Calendar Project

step 20 - instructs to check: if len(calendar.keys()) < 1 - to determine if calendar is not empty.

step 38 - instructs to iterate through calendar.keys() - for next steps looking for a value match.

These lines execute the same by my testing when calendar.keys() is replaced with just calendar.

i.e. if len(calendar) < 1: or for date in calendar:


Why do we chose to reference calendar.keys() instead of just calendar? Are these equivalent in this situation; or is referencing .keys() better somehow?

tags: Lists, keys,

Not better, just specific. It is a direct reference.


Do the non-direct reference methods eventually slow things down with a complex script?

(Assuming yes)

I can’t honestly say how perfomance will be impacted. dict.keys() is a simpler data structure to traverse than a dictionary, so one could surmise better performance. In this instance I don’t believe it matters as much as adding a new concept to our toolbox.

i find a difference in performance with step 38.
When i delete an event in a for loop with the dictonary reference i get a runtime error: directory changed size during iteration.
When i do the same with a dict.keys() reference. it doesn’t happen.
i can’t conceptualize why. does anyone understand?