-4

I have a list that has several empty lists inside it. I followed the directions here (Python: How to remove empty lists from a list?) in an attempt to filter out the empty lists but failed and I really have no reason why.

My list:

data: [['#0721', '', '', '', ''], ['', '', '', '', ''], ['', '', '', '', ''], ['GBE COD', '746', '$2.00', '', '$1,492.00'], ['GBW COD', '13,894', '$0.50', '', '$6,947.00'], ['GOM COD', '60', '$2.00', '', '$120.00'], ['GB WINTER FLOUNDER', '94,158', '$0.25', '', '$23,539.50'], ['GOM WINTER FLOUNDER', '3,030', '$0.50', '', '$1,515.00'], ['GBE HADDOCK', '18,479', '$0.02', '', '$369.58'], ['GOM HADDOCK', '0', '$0.02', '', '$0.00'], ['GBW HADDOCK', '110,470', '$0.02', '', '$2,209.40'], ['HAKE', '259', '$1.30', '', '$336.70'], ['PLAICE', '3,738', '$0.40', '', '$1,495.20'], ['POLLOCK', '3,265', '$0.02', '', '$65.30'], ['WITCH FLOUNDER', '1,134', '$1.30', '', '$1,474.20'], ['SNE YT', '1,458', '$0.65', '', '$947.70'], ['GB YT', '4,499', '$0.70', '', '$3,149.30'], ['REDFISH', '841', '$0.02', '', '$16.82'], ['', '', '', '', ''], ['', '', '', '', ''], ['54 DAS @ $8.00/DAY = 432.00', '', ''], ['', '', '', '', '', '', '', '', ''], ['', '', '', '', '', '', '', '', '']]

My code:

data = filter(None, data)
print("second data:", data)
data = list(data)
print("third data:", data)

Which produces:

second data: <filter object at 0x05F23CF0>
third data: [['#0721', '', '', '', ''], ['', '', '', '', ''], ['', '', '', '', ''], ['GBE COD', '746', '$2.00', '', '$1,492.00'], ['GBW COD', '13,894', '$0.50', '', '$6,947.00'], ['GOM COD', '60', '$2.00', '', '$120.00'], ['GB WINTER FLOUNDER', '94,158', '$0.25', '', '$23,539.50'], ['GOM WINTER FLOUNDER', '3,030', '$0.50', '', '$1,515.00'], ['GBE HADDOCK', '18,479', '$0.02', '', '$369.58'], ['GOM HADDOCK', '0', '$0.02', '', '$0.00'], ['GBW HADDOCK', '110,470', '$0.02', '', '$2,209.40'], ['HAKE', '259', '$1.30', '', '$336.70'], ['PLAICE', '3,738', '$0.40', '', '$1,495.20'], ['POLLOCK', '3,265', '$0.02', '', '$65.30'], ['WITCH FLOUNDER', '1,134', '$1.30', '', '$1,474.20'], ['SNE YT', '1,458', '$0.65', '', '$947.70'], ['GB YT', '4,499', '$0.70', '', '$3,149.30'], ['REDFISH', '841', '$0.02', '', '$16.82'], ['', '', '', '', ''], ['', '', '', '', ''], ['54 DAS @ $8.00/DAY = 432.00', '', ''], ['', '', '', '', '', '', '', '', ''], ['', '', '', '', '', '', '', '', '']]

So it didn't end up doing anything, why is that?

halfer
  • 19,824
  • 17
  • 99
  • 186
theprowler
  • 3,138
  • 11
  • 28
  • 39
  • 1
    I don't see any empty lists here. A list with 5 empty strings in it still has 5 things in it. – user2357112 Jun 01 '17 at 17:47
  • Hi there. If you'd like to answer, please add it below - click on 'Answer Your Question'. We do not add solutions as edits to the question here. – halfer Jun 05 '17 at 11:50

2 Answers2

2

You can use any(a_list), which is True if any element of a_list evaluates to True, as non-empty strings do.

data = [['#0721', '', '', '', ''], ['', '', '', '', ''], ['', '', '', '', ''], ['GBE COD', '746', '$2.00', '', '$1,492.00'], ['GBW COD', '13,894', '$0.50', '', '$6,947.00'], ['GOM COD', '60', '$2.00', '', '$120.00'], ['GB WINTER FLOUNDER', '94,158', '$0.25', '', '$23,539.50'], ['GOM WINTER FLOUNDER', '3,030', '$0.50', '', '$1,515.00'], ['GBE HADDOCK', '18,479', '$0.02', '', '$369.58'], ['GOM HADDOCK', '0', '$0.02', '', '$0.00'], ['GBW HADDOCK', '110,470', '$0.02', '', '$2,209.40'], ['HAKE', '259', '$1.30', '', '$336.70'], ['PLAICE', '3,738', '$0.40', '', '$1,495.20'], ['POLLOCK', '3,265', '$0.02', '', '$65.30'], ['WITCH FLOUNDER', '1,134', '$1.30', '', '$1,474.20'], ['SNE YT', '1,458', '$0.65', '', '$947.70'], ['GB YT', '4,499', '$0.70', '', '$3,149.30'], ['REDFISH', '841', '$0.02', '', '$16.82'], ['', '', '', '', ''], ['', '', '', '', ''], ['54 DAS @ $8.00/DAY = 432.00', '', ''], ['', '', '', '', '', '', '', '', ''], ['', '', '', '', '', '', '', '', '']]

non_empty = [sublist for sublist in data if any(sublist)]
print(non_empty)

[['#0721', '', '', '', ''],
 ['GBE COD', '746', '$2.00', '', '$1,492.00'],
 ['GBW COD', '13,894', '$0.50', '', '$6,947.00'],
 ['GOM COD', '60', '$2.00', '', '$120.00'],
 ['GB WINTER FLOUNDER', '94,158', '$0.25', '', '$23,539.50'],
 ['GOM WINTER FLOUNDER', '3,030', '$0.50', '', '$1,515.00'],
 ['GBE HADDOCK', '18,479', '$0.02', '', '$369.58'],
 ['GOM HADDOCK', '0', '$0.02', '', '$0.00'],
 ['GBW HADDOCK', '110,470', '$0.02', '', '$2,209.40'],
 ['HAKE', '259', '$1.30', '', '$336.70'],
 ['PLAICE', '3,738', '$0.40', '', '$1,495.20'],
 ['POLLOCK', '3,265', '$0.02', '', '$65.30'],
 ['WITCH FLOUNDER', '1,134', '$1.30', '', '$1,474.20'],
 ['SNE YT', '1,458', '$0.65', '', '$947.70'],
 ['GB YT', '4,499', '$0.70', '', '$3,149.30'],
 ['REDFISH', '841', '$0.02', '', '$16.82'],
 ['54 DAS @ $8.00/DAY = 432.00', '', '']]


Thierry Lathuille
  • 23,663
  • 10
  • 44
  • 50
0

As user2357112 has pointed out you don't have empty lists, you have lists of the string '' whicch is not am empty list.

To solve your problem you should change the first parameter you are passing to filter. filter(function, iterable) will return a list with values from iterable that functions returns true.

On your case, try:

def isEmpty(list):
    return any(list)

print filter(isEmpty, data)
Pedro Henrique
  • 601
  • 6
  • 17