Best way to clean up list items?

Peter Otten __peter__ at web.de
Mon May 2 13:30:41 EDT 2016


DFS wrote:

> Have: list1 = ['\r\n   Item 1  ','  Item 2  ','\r\n  ']
> Want: list1 = ['Item 1','Item 2']
> 
> 
> I wrote this, which works fine, but maybe it can be tidier?
> 
> 1. list2 = [t.replace("\r\n", "") for t in list1]   #remove \r\n
> 2. list3 = [t.strip(' ') for t in list2]            #trim whitespace
> 3. list1  = filter(None, list3)                     #remove empty items
> 
> 
> After each step:
> 
> 1. list2 = ['   Item 1  ','  Item 2  ','  ']   #remove \r\n
> 2. list3 = ['Item 1','Item 2','']              #trim whitespace
> 3. list1 = ['Item 1','Item 2']                 #remove empty items
> 
> 
> Thanks!

s.strip() strips all whitespace, so you can combine steps 1 and 2:

>>> items = ['\r\n   Item 1  ','  Item 2  ','\r\n  ']
>>> stripped = (s.strip() for s in items)

The (...) instead of [...] denote a generator expression, so the iteration 
has not started yet. The final step uses a list comprehension instead of 
filter():

>>> [s for s in stripped if s]
['Item 1', 'Item 2']

That way the same code works with both Python 2 and Python 3. Note that you 
can iterate over the generator expression only once; if you try it again 
you'll end empty-handed:

>>> [s for s in stripped if s]
[]

If you want to do it in one step here are two options that both involve some 
duplicate work:

>>> [s.strip() for s in items if s and not s.isspace()]
['Item 1', 'Item 2']
>>> [s.strip() for s in items if s.strip()]
['Item 1', 'Item 2']





More information about the Python-list mailing list