[Tutor] user-given variable names for objects

Tiger12506 keridee at jayco.net
Thu Dec 13 22:13:32 CET 2007


I may sound like a know-it-all, but dictionaries *are* iterators.

[a for a in eventData if eventData[a] < time.time()]

This is more efficient. The keys method creates a list in memory first and 
then it iterates over it.
Unnecessary.

>
> "Che M" <pine508 at hotmail.com> wrote
>
>>  Although I was not familiar with what you can do with a list such
>> as you did here:
>> [a for a in eventData.keys() if eventData[a] < time.time()]
>
> This is known as a list comprehension (and is described in the
> Functional
> Programming topic of my tutorial - obligatory plug :-)
>
>> I guess .keys() is a built-in method for dictionaries to return a
>> list of all their values, then?
>
> To be accurate it returns a list of the keys, the values are the
> things you get when you access the dictionary using a key:
>
> value = dictionary[key]
>
> So you can do things like
>
> for key in dictionary.keys():
>    print dictionary[key]
>
>> By the way, what was the purpose of the line with
>> time.sleep(1)
>
> It pauses for 1 second. But i'm not sure why he wanted a pause! :-)
>
> Alan G.
>



More information about the Tutor mailing list