Efficient Find and Replace

Fredrik Lundh fredrik at pythonware.com
Sat Jan 28 01:38:59 EST 2006


Raymond Hettinger wrote:

> > 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)

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"...

</F>






More information about the Python-list mailing list