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

2006-10-21 Thread Kent Johnson
Jason Massey wrote: > Why not: > > if item in lst: > loc = lst.index(item) > lst[loc] = str You can also just try to do the replacement and catch the ValueError that is raised if the item is not there: try: loc = list.index(item) list[loc] = str except ValueError: pass If lst is lon

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

2006-10-20 Thread Chris Hengge
I like it because it different.. and it reads cleanly... =PAs far as the first occurance.. I'm not concerned about checking extra, because the first occurance is the only one I should ever need. On 10/20/06, Luke Paireepinart <[EMAIL PROTECTED]> wrote: Chris Hengge wrote:> I'm trying to build a li

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

2006-10-20 Thread Luke Paireepinart
Chris Hengge wrote: > 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 >

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

2006-10-20 Thread Alan Gauld
Chris, > Will that replace the location? or add to it? > >> if item in list: >> loc = list.index(item) >> list[loc] = str Jason already showed you the answer, but significantly he also showed you how to find out for yourself. Use the >>> prompt. Its what its there for. For some reason peopl

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

2006-10-20 Thread Jason Massey
You can access and change the elements in a list by directly referencing their position in the list.Something like:>>> foo = [1,2,3]>>> foo[0]1>>> foo[0] = 'a'>>> foo ['a', 2, 3]On 10/20/06, Chris Hengge <[EMAIL PROTECTED]> wrote: Will that replace the location? or add to it? Thanks.On 10/20/06, Ja

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

2006-10-20 Thread Chris Hengge
Will that replace the location? or add to it? Thanks.On 10/20/06, Jason Massey <[EMAIL PROTECTED] > wrote:Why not:if item in list: loc = list.index(item)  list[loc] = strOn 10/20/06, Chris Hengge < [EMAIL PROTECTED]> wrote: I'm trying to build a little piece of code that replaces an item in a list

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

2006-10-20 Thread Jason Massey
Why not:if item in list: loc = list.index(item) list[loc] = strOn 10/20/06, Chris Hengge < [EMAIL PROTECTED]> wrote: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

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

2006-10-20 Thread Chris Hengge
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 ite