[Tutor] user-given variable names for objects
andreas at kostyrka.org
Thu Feb 28 09:58:00 CET 2008
-----BEGIN PGP SIGNED MESSAGE-----
Perhaps even nicer:
[key for key, evtime in eventData.iteritems() if evtime < time.time()]
This way the dictionary iterates over key, value tuples.
| 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.
|> "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
|> 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
|> It pauses for 1 second. But i'm not sure why he wanted a pause! :-)
|> Alan G.
| Tutor maillist - Tutor at python.org
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org
-----END PGP SIGNATURE-----
More information about the Tutor