[Tutor] user-given variable names for objects

Andreas Kostyrka andreas at kostyrka.org
Thu Feb 28 09:58:00 CET 2008

Hash: SHA1

Perhaps even nicer:

[key for key, evtime in eventData.iteritems() if evtime < time.time()]

This way the dictionary iterates over key, value tuples.


Tiger12506 wrote:
| 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
| 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.
| _______________________________________________
| Tutor maillist  -  Tutor at python.org
| http://mail.python.org/mailman/listinfo/tutor
Version: GnuPG v1.4.2 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org


More information about the Tutor mailing list