0

i have this list that contains an empty element:

list = ['Caramanico Terme', ' ', 'Castellafiume', ' ', 'Castelvecchio Subequo',    ' ', 'Falesia di ovindoli', ' ', 'Fara San Martino', ' ', "L'Aquila - Madonna      d'Appari", ' ', 'La Palma Pazza (Bisegna AQ)', ' ', 'Liscia Palazzo', ' ', 'Luco dei marsi', ' ', 'Montebello di Bertona', ' ', 'Monticchio', ' ', 'Palena', ' ', 'Pennadomo', ' ', 'Pennapiedimonte', ' ', 'Pescomarrino', ' ', 'Petrella', ' ', 'Pianezza', ' ', 'Pietrasecca', ' ', ' ', 'PietrePiane', ' ', 'Pizzi di Lettopalena (loc. Fonte della Noce)', ' ', 'Placche di Bini', ' ', 'Roccamorice', ' ', 'Sasso di Lucoli', ' ', 'Villetta Barrea', ' ']

how i can remove this '' empty element? I have try in this way:

[x for x in list if all(x)]

but the elements are not delete Any help? Thanks

1
  • Have you looked at Array.pop "array.pop([i]) Removes the item with the index i from the array and returns it. The optional argument defaults to -1, so that by default the last item is removed and returned." docs.python.org/2/library/array.html Commented May 3, 2017 at 13:25

4 Answers 4

10

First of all. Make sure to not call your list list. That's a built-in type and will cause problems later. I renamed it to lst. Then you can filter the list the following way:

lst = ['Caramanico Terme', ' ', 'Castellafiume', ' ', 'Castelvecchio Subequo',    ' ', 'Falesia di ovindoli', ' ', 'Fara San Martino', ' ', "L'Aquila - Madonna      d'Appari", ' ', 'La Palma Pazza (Bisegna AQ)', ' ', 'Liscia Palazzo', ' ', 'Luco dei marsi', ' ', 'Montebello di Bertona', ' ', 'Monticchio', ' ', 'Palena', ' ', 'Pennadomo', ' ', 'Pennapiedimonte', ' ', 'Pescomarrino', ' ', 'Petrella', ' ', 'Pianezza', ' ', 'Pietrasecca', ' ', ' ', 'PietrePiane', ' ', 'Pizzi di Lettopalena (loc. Fonte della Noce)', ' ', 'Placche di Bini', ' ', 'Roccamorice', ' ', 'Sasso di Lucoli', ' ', 'Villetta Barrea', ' ']
filtered = [x for x in lst if len(x.strip()) > 0]

This will remove all kinds of whitepace elements like ' ' or ' ' etc.

EDIT:

As corn3lius pointed out, this would work too:

filtered = [x for x in lst if x.strip()]
Sign up to request clarification or add additional context in comments.

1 Comment

alternatively you could just do this filtered = [x for x in lst if x.strip()]
0

You can add a condition in comprehension list:

l = ['Caramanico Terme', ' ', 'Castellafiume', ' ', 'Castelvecchio Subequo',    ' ', 'Falesia di ovindoli', ' ', 'Fara San Martino', ' ', "L'Aquila - Madonna      d'Appari", ' ', 'La Palma Pazza (Bisegna AQ)', ' ', 'Liscia Palazzo', ' ', 'Luco dei marsi', ' ', 'Montebello di Bertona', ' ', 'Monticchio', ' ', 'Palena', ' ', 'Pennadomo', ' ', 'Pennapiedimonte', ' ', 'Pescomarrino', ' ', 'Petrella', ' ', 'Pianezza', ' ', 'Pietrasecca', ' ', ' ', 'PietrePiane', ' ', 'Pizzi di Lettopalena (loc. Fonte della Noce)', ' ', 'Placche di Bini', ' ', 'Roccamorice', ' ', 'Sasso di Lucoli', ' ', 'Villetta Barrea', ' ']

print([l for l in list if l != ' '])

Comments

0

Removing all items that not is ' ' i.e. the empty string is the same thing as building a set with all elements from the first set that has length > 0. This one liner takes care of that:

a = ['', 'apple', '', 'peach']
b = [i for i in a if i != '']

Comments

0

Removing empty items from list. Here empty items might be in single space or multiple space within quotes. So, use strip() function in list comprehension.

Ex:

temp_str = '      || 0X0C    ||   0X00000   ||   0X00094   ||   0X00E8C   ||           IN_OPER   ||  000000e8cff7e000 || '
temp_str.split('||')
# result: ['      ', ' 0X0C    ', '   0X00000   ', '   0X00094   ', '   0X00E8C   ', '           IN_OPER   ', '  000000e8cff7e000 ', ' ']

temp_list = [ x for x in temp_str.split('||') if x]
temp_list
# result: ['      ', ' 0X0C    ', '   0X00000   ', '   0X00094   ', '   0X00E8C   ', '           IN_OPER   ', '  000000e8cff7e000 ', ' ']

temp_list = [ x for x in temp_str.split('||') if x.strip()]
temp_list
# result: [' 0X0C    ', '   0X00000   ', '   0X00094   ', '   0X00E8C   ', '           IN_OPER   ', '  000000e8cff7e000 ']

temp_list = [ x.strip() for x in temp_str.split('||') if x.strip()]
temp_list
# result: ['0X0C', '0X00000', '0X00094', '0X00E8C', 'IN_OPER', '000000e8cff7e000']

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.