[Tutor] Iterating a dict with an iteration counter? How would *you* do it?

Oscar Benjamin oscar.j.benjamin at gmail.com
Tue Feb 5 12:27:22 CET 2013


On 5 February 2013 03:56, eryksun <eryksun at gmail.com> wrote:
> On Mon, Feb 4, 2013 at 7:04 PM, Dave Angel <davea at davea.name> wrote:
>>> Nope, in both Python 2 and 3 iterating over a dict directly just
>>> provides the key. That's also how "if key in dict" works.
>
> A dict implements __contains__ for an efficient "in" test. In general,
> the interpreter falls back to using iteration if a type lacks
> __contains__.

I almost wrote this response but then I realised that Dave probably
meant that "obj in dict" returns True if the dict has a key equal to
obj rather than if the dict has a (key, value) pair equal to obj.


Oscar


More information about the Tutor mailing list