[Tutor] Do something on list elements

Valerio Pachera valerio at pbds.eu
Mon Jul 30 06:37:04 EDT 2018

----- Messaggio originale -----
> Da: "Tutor Python" <tutor at python.org>
> A: "Tutor Python" <tutor at python.org>
> 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','') for s in l)
> print(l)

Thank you all for the answers.
List comprehension is what I was looking for.

Here's almost the same example that I made up to summarize:

>>> l = ['one ', 'two ', 'three ']
>>> print(l)
['one ', 'two ', 'three ']

>>> l = [ s.strip() for s in l]
>>> print(l)
['one', 'two', 'three']

I create a new list with the same name so I don't need to have another list.

More information about the Tutor mailing list