Re: [Tutor] changing list element in loop

2013-04-27 Thread Alan Gauld
On 27/04/13 10:49, Jim Mooney wrote: Why isn't 'e' changing to 'pP here when the vowel list is mutable: vowelList = list('aeiouy') for x in vowelList: if x == 'e': x = 'P' x is just a reference to the object in the list. When you assign 'P' to x you are making x point at a new

Re: [Tutor] changing list element in loop

2013-04-27 Thread Steven D'Aprano
Oh, and another thing... On 27/04/13 20:31, Jim Mooney wrote: What's the simplest way to go through a list, find something, and replace it with something else? The simplest way is not to do it at all. Instead, create a new list: vowels = 'aeiouy' result = [] for char in vowels: if char

Re: [Tutor] changing list element in loop

2013-04-27 Thread Steven D'Aprano
On 27/04/13 20:31, Jim Mooney wrote: Okay, I tried enumerate, but now I get an "immutable" error. So let's turn this around since it's getting out of hand for a simple list replacement ;') What's the simplest way to go through a list, find something, and replace it with something else? vowels

Re: [Tutor] changing list element in loop

2013-04-27 Thread Amit Saha
On Sat, Apr 27, 2013 at 8:31 PM, Jim Mooney wrote: > On 27 April 2013 02:55, Amit Saha wrote: >> On Sat, Apr 27, 2013 at 7:49 PM, Jim Mooney wrote: >>> Why isn't 'e' changing to 'pP here when the vowel list is mutable: >>> >>> vowelList = list('aeiouy') >>> >>> for x in vowelList: >>> if x =

Re: [Tutor] changing list element in loop

2013-04-27 Thread Jim Mooney
On 27 April 2013 02:55, Amit Saha wrote: > On Sat, Apr 27, 2013 at 7:49 PM, Jim Mooney wrote: >> Why isn't 'e' changing to 'pP here when the vowel list is mutable: >> >> vowelList = list('aeiouy') >> >> for x in vowelList: >> if x == 'e': >> x = 'P' > > This is because x is really a l

Re: [Tutor] changing list element in loop

2013-04-27 Thread Amit Saha
On Sat, Apr 27, 2013 at 7:49 PM, Jim Mooney wrote: > Why isn't 'e' changing to 'pP here when the vowel list is mutable: > > vowelList = list('aeiouy') > > for x in vowelList: > if x == 'e': > x = 'P' This is because x is really a label for the item in your list. It does not represent

[Tutor] changing list element in loop

2013-04-27 Thread Jim Mooney
Why isn't 'e' changing to 'pP here when the vowel list is mutable: vowelList = list('aeiouy') for x in vowelList: if x == 'e': x = 'P' print(vowelList) #result: ['a', 'e', 'i', 'o', 'u', 'y'] -- Jim Mooney ___ Tutor maillist - Tutor@pyt