[Tutor] dictionary swittching
Kalle Svensson
kalle@lysator.liu.se
Tue, 14 May 2002 13:04:34 +0200
[Jörg Wölke]
> if "a" in x.keys():
> print "Does that do what you want?"
Don't do that, it's O(N). First it gets all the keys from the dict,
which is linear time. Then it does a linear search through the
resulting list to see if "a" is a member. Use
if x.has_key("a"):
which is O(1), it uses hashing.
if "a" in x:
does the right thing in Python 2.2.
Peace,
Kalle
--
Kalle Svensson, http://www.juckapan.org/~kalle/
Student, root and saint in the Church of Emacs.