Re: [Tutor] Do something on list elements

2018-07-30 Thread Valerio Pachera
- Messaggio originale - > Da: "Tutor Python" > A: "Tutor Python" > Inviato: Sabato, 28 luglio 2018 0:06:55 > Oggetto: Re: [Tutor] Do something on list elements > But better still is a list comprehension: > > l = [s.replace('X','

Re: [Tutor] Do something on list elements

2018-07-27 Thread Mats Wichmann
On 07/27/2018 04:32 PM, Cameron Simpson wrote: > On 27Jul2018 23:06, Alan Gauld wrote: >> In Python you very rarely need to resort to using indexes >> to process the members of a collection. And even more rarely >> do you need to manually increment the index. I think this was an important point:

Re: [Tutor] Do something on list elements

2018-07-27 Thread Alan Gauld via Tutor
On 27/07/18 23:32, Cameron Simpson wrote: >> for index, s in l: >> l[index] = s.replace('X','') >> print(l) > > I think you meant: > > for index, s in enumerate(l): Oops, yes. Sorry. >> In Python you very rarely need to resort to using indexes >> to process the members of a collection. And

Re: [Tutor] Do something on list elements

2018-07-27 Thread Cameron Simpson
On 27Jul2018 23:06, Alan Gauld wrote: On 27/07/18 13:56, Valerio Pachera wrote: l = ['unoX', 'dueX'] c = 0 for n in l: l[c] = l[c].replace('X','') c = c + 1 print (l) it works but I wonder if there's a better way to achieve the same. Yes, a much better way. for index, s in

Re: [Tutor] Do something on list elements

2018-07-27 Thread Alan Gauld via Tutor
On 27/07/18 13:56, Valerio Pachera wrote: > l = ['unoX', 'dueX'] > c = 0 > for n in l: > l[c] = l[c].replace('X','') > c = c + 1 > print (l) > --- > > it works but I wonder if there's a better way to achieve the same. Yes, a much better way. for index, s in l: l[index] = s.re