On Mar 27, 11:20 am, Paul Rubin <http://phr...@nospam.invalid> wrote:
> Carl Banks <pavlovevide...@gmail.com> writes:
> > >      if x in theDict:
> > >           print x, v
>
> > Where does v come from?
>
> Oops, pasted from original.  Meant of course "print x, theDict[x]".

You have look up x twice with that code, whereas you wouldn't have to
with this:

v = theDict.get(x)
if v is not None:
    print x, v

or with this:

try:
    v = theDict[x]
except KeyError:
    pass
else:
    print x,v

Also, these minimize the number of times you have to type x.  Thus I
recommend either of these two, or a defaultdict, for this.


Carl Banks
--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to