Avoid iterating over a list while doing the changes on it:
data=[[],[],[],[],[]]
for rows in data:
if len(rows)==0:
data.remove(rows)
print data
Instead, create a new list:
>>> lst = []
>>> data = [[],[],[],[1,2,3]]
>>>
>>> for rows in data:
if rows: #if rows is not an empty list
lst.append(rows)
>>> lst
[[1, 2, 3]]
>>>
Same as you did with filter
which creates a new object:
>>> lst = [[],[],[],[1,2,3],[1,],[], [4,5,6],[7]]
>>> lst = filter(list.__len__,lst)
>>> lst
[[1, 2, 3], [1], [4, 5, 6], [7]]
EDIT:
After doing some profiling, I got the following resutls:
>>> import timeit
>>> timeit.timeit('filter(list.__len__, lst)', setup='lst=[[],[],[],[1,2,3],[1,],[], [4,5,6],[7]]', number=1000000)
1.03789886128774
>>>
>>> timeit.timeit('filter(lambda x:x, lst)', setup='lst=[[],[],[],[1,2,3],[1,],[], [4,5,6],[7]]', number=1000000)
1.0035609489573218
>>>
>>> timeit.timeit('filter(None, lst)', setup='lst=[[],[],[],[1,2,3],[1,],[], [4,5,6],[7]]', number=1000000)
0.4335933814062045
>>>
>>> timeit.timeit('[l for l in lst if l]', setup='lst=[[],[],[],[1,2,3],[1,],[], [4,5,6],[7]]', number=1000000)
0.41073885410420985