[Tutor] Best way to replace items in a list.

Chris Hengge pyro9219 at gmail.com
Fri Oct 20 22:00:03 CEST 2006


I'm trying to build a little piece of code that replaces an item in a list.

Here is a sample of what I'd like to do.

str = "This was replaced"

ff item in list:
   replace item with str

I know I can do list.remove(item), but how do I place str back into that
exact location?

Is this how / best way?

if item in list:
   loc = list.index(item)
   list.remove(item)
   list.insert(loc, str)

Thanks.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20061020/6afaa45e/attachment.htm 


More information about the Tutor mailing list