[Tutor] How to remove all empty elements from a list of list

Pascal Maniriho manpasco1 at gmail.com
Wed Nov 17 08:51:49 EST 2021


Dear all,

I have the following list in Python and would like to remove all empty
elements without invalidating indices.

mylist = [['write', 'create', 'draw', 'book', 'mkay', '', '', '', '', '',
''], ['hey', 'mykey', 'ange', 'kiki', 'rose', '', '', '', '', '', '', '',
'', '', ''],['name','age','address', 'nationality', '', '', '', '', '', '',
'', '', '', '', '', '']]

I need the output like this:

mylist = [['write', 'create', 'draw', 'book', 'mkay'], ['hey', 'mykey',
'ange', 'kiki', 'rose']]

I have tried to use the following line of code, but it does not give the
expected output.

mylist1 = list(filter(None, mylist))


Kindly help

......

pasco


More information about the Tutor mailing list