Fast Dictionary Access
clp2 at rebertia.com
Sat Jun 27 05:53:40 EDT 2009
On Sat, Jun 27, 2009 at 2:47 AM, Thomas
Lehmann<Iris-und-Thomas-Lehmann at t-online.de> wrote:
> In C++, programming STL you will use the insert method which always
> provides a position and a flag which indicates whether the position
> results from a new insertion or an exisiting element. Idea is to have
> one search only.
> if data.has_key(key):
> value = data[key]
> But this does mean (does it?) that the dictionary is searched two
> times! If so, can somebody show me how to do this in one step?
Use exception handling:
value = data[key]
print "No such key"
print "The value for that key is", value
More information about the Python-list