Reputation: 52318
I want to process a list of elements ls
, each of which may or may not have a corresponding element in a dictionary d
. I can think of two ways to do it. Here, s
is the set of all elements that are keys of d
. When constructing d
it is trivial to construct s
as well, so take that as given.
for e in ls:
if e in s:
process(d[e])
and
for e in ls:
try:
process(d[e])
except KeyError:
pass
Of the two, which is faster? Furthermore I've heard that Python uses the principle of "Ask forgiveness, not permission". Does this mean that in general testing using if
statements will be slower than using exceptions?
Upvotes: 1
Views: 104
Reputation: 1122082
If order doesn't matter, use intersections:
for e in s.intersection(ls):
# only elements that are in `s` *and* `ls` are iterated over
process(d[e])
Most of your time is probably going to be spent in process()
in any case, so don't micro-optimize. Instead optimize for readability (within reason).
As for the choice of in
test vs. exception handling: exception handling will be faster if the number of misses is low (relatively few exceptions), but if you have a lot of misses the in
test will be faster than handling loads of exceptions. If you really care, use the timeit
module to find a balance. See my answer on the subject on Programmers.SE.
Upvotes: 4