-2

I tried different way to remove empty element to nested list, but I don't know if the best way on 100k list.

Example:

tag = [['A', 'B', 'C', ''], ['D', 'F', 'G', '', '',''], ['H','I','J'],['L','M','','','','']]

I find different approaches like:

list(filter(None, tag))

or

for i in tag:
    test_list = [i for i in tag if i]

or

'' in [j for i in tag for j in i]

No way, I cannot remove empty element, first, and second, time to achieve it, is too slow. Also I tried to develop few nested list on python tutor, but don't remove anything.

Suggestion please? thank you

Ch3steR
  • 20,090
  • 4
  • 28
  • 58
MarcusRB
  • 58
  • 7

2 Answers2

2

I don't know if performance would satisfy, but

tags = [['A', 'B', 'C', ''], ['D', 'F', 'G', '', '',''],
        ['H','I','J'],['L','M','','','','']]
filtered_tags = [[tag for tag in item if tag] for item in tags]
print(filtered_tags)

output

[['A', 'B', 'C'], ['D', 'F', 'G'], ['H', 'I', 'J'], ['L', 'M']]
buran
  • 13,682
  • 10
  • 36
  • 61
2

You can use filter with list comprehension.

[list(filter(None,l)) for l in tag]
# [['A', 'B', 'C'], ['D', 'F', 'G'], ['H', 'I', 'J'], ['L', 'M']]

Or

[[i for i in l if i] for l in tag]
# [['A', 'B', 'C'], ['D', 'F', 'G'], ['H', 'I', 'J'], ['L', 'M']]

Some timeit analysis using Python 3.7 windows 10.

#Using list of size 100_000 i.e len(tag)=100_000 for benchmarking
In [12]: timeit [list(filter(None,l)) for l in tag]
42.1 ms ± 1.38 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)

In [13]: timeit [[i for i in l if i] for l in tag]
34.7 ms ± 733 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)
Ch3steR
  • 20,090
  • 4
  • 28
  • 58