[Tutor] use of the newer dict types

Albert-Jan Roskam fomcl at yahoo.com
Fri Jul 26 10:50:45 CEST 2013


<snip>
>In Python 2, the preferred way to iterate over values or key/value pairs was to use an iterator, since that is more light-weight than a list:
>
>for value in mydict.itervalues(): ...

if I have d = {1: 2}, I can do membership testing in the following two ways --but which one is the preferred way?:
if 1 in d:
    pass
if d.has_key(1):
     pass
I find "in" (__contains__) more readable, but maybe has_key has advantages (speed)?


More information about the Tutor mailing list