I like it because it different.. and it reads cleanly... =P<br>As far as the first occurance.. I'm not concerned about checking extra, because the first occurance is the only one I should ever need. <br><br><div><span class="gmail_quote">
On 10/20/06, <b class="gmail_sendername">Luke Paireepinart</b> <<a href="mailto:rabidpoobear@gmail.com">rabidpoobear@gmail.com</a>> wrote:</span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Chris Hengge wrote:<br>> I'm trying to build a little piece of code that replaces an item in a<br>> list.<br>><br>> Here is a sample of what I'd like to do.<br>><br>> str = "This was replaced"<br>
><br>> ff item in list:<br>> replace item with str<br>><br>> I know I can do list.remove(item), but how do I place str back into<br>> that exact location?<br>><br>> Is this how / best way?<br>This is almost definitely not the best way to do that, though it depends
<br>what results you're looking for.<br>This way will only replace the first occurrence of the item.<br>I don't know why you like the 'if item in ...' syntax so much ( ;) ),<br>but you could do this with a loop pretty easily.
<br>#example<br>for index,item in enumerate(lst):<br> if item == 'Item To Replace':<br> lst[index] = 'Replaced!'<br>#--------<br>HTH,<br>-Luke<br>><br>> if item in list:<br>> loc = list.index(item)<br>
> list.remove(item)<br>> list.insert(loc, str)<br>><br>> Thanks.<br>> ------------------------------------------------------------------------<br>><br>> _______________________________________________
<br>> Tutor maillist - <a href="mailto:Tutor@python.org">Tutor@python.org</a><br>> <a href="http://mail.python.org/mailman/listinfo/tutor">http://mail.python.org/mailman/listinfo/tutor</a><br>><br><br></blockquote>
</div><br>