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


#1

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:

Question:

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,


#2

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


#3

Thanks!

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

(Assuming yes)


#4

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.