> > > for i,v in enumerate(L):
> > >     if v == X:
> > >         L[i] = Y
> >
> > Here's an alternate solution using a replacement dictionary:
> >
> >   M = {X:Y}
> >   for i, v in enumerate(L):
> >       L[i] = M.get(v, v)

[Fredrik Lundh]
> but that's 2-3 times slower than the OP's corrected code for his use
> case, so I'm not sure it qualifies as more "efficient"...

The alternate wasn't presented for efficiency.  Its virtue is that with
no additional effort, it generalizes to substituting multiple
find/replace pairs in a single pass.


Raymond

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

Reply via email to