Where Are My Keys?

This post originally appeared on the Software Carpentry website.

I was looking through some Python code a few days ago, and noticed that its author was using this:

if something in dict.keys():
    dict[something] += 1

instead of:

if something in dict:
    dict[something] += 1

It seems like a small difference, but it's actually a very important one. The second form checks to see whether something is a key in the dictionary dict. The first form, on the other hand, creates a list of all the keys in the dictionary, then searches that list from start to finish to see if something is there. They both produce the right answer, but the fast form does it in constant time, while the slow form (the one with the call to dict.keys()) takes time proportional to the size of the dictionary. The really bad news is, the "error" is silent.

What other examples have you seen of people getting the right answer the wrong way? And what more could we do to teach people how to avoid these traps? Does it have to be done case by case, or is there something larger or more general we could try to convey?

Dialogue & Discussion

Comments must follow our Code of Conduct.

Edit this page on Github