0

For some reason I can't get the filter function to work. I'm trying to remove empty strings from a list. After reading Remove empty strings from a list of strings, I'm trying to utilize the filter function.

import csv
import itertools

importfile = raw_input("Enter Filename(without extension): ")
importfile += '.csv'
test=[]
#imports plant names, effector names, plant numbers and leaf numbers from csv file
with open(importfile) as csvfile:
    lijst = csv.reader(csvfile, delimiter=';', quotechar='|')
    for row in itertools.islice(lijst, 0, 4):
        test.append([row])

test1 = list(filter(None, test[3]))
print test1

This however returns:

[['leafs', '3', '', '', '', '', '', '', '', '', '', '']]

What am I doing wrong?

Community
  • 1
  • 1

3 Answers3

0

You filter a list of lists, where the inner item is a non-empty list.

>>> print filter(None, [['leafs', '3', '', '', '', '', '', '', '', '', '', '']])
[['leafs', '3', '', '', '', '', '', '', '', '', '', '']]

If you filter the inner list, the one that contains strings, everything works as expected:

>>> print filter(None, ['leafs', '3', '', '', '', '', '', '', '', '', '', ''])
['leafs', '3']
9000
  • 39,899
  • 9
  • 66
  • 104
0

You have a list in a list, so filter(None, ...) is applied on the non-empty list, the empty strings are not affected. You can use, say a nested list comprehension to reach into the inner list and filter out falsy object:

lst = [['leafs', '3', '', '', '', '', '', '', '', '', '', '']]

test1 = [[x for x in i if x] for i in lst]
# [['leafs', '3']]
Moses Koledoye
  • 77,341
  • 8
  • 133
  • 139
0

I was indeed filtering lists of lists, the problem in my code was:

    for row in itertools.islice(lijst, 0, 4):
    test.append[row]

This should be:

for row in itertools.islice(lijst, 0, 4):
        test.append(row)