如何完全删除列表

时间:2016-11-09 23:12:29

标签: list python-3.x sublist

我有一个与此类似的列表

numbers=[['3','4','5'],['',''],['6','7','8']

我想删除

['','']
完全

子列表,所以最终的数字列表应该是

numbers=[['3','4','5'],['6','7','8']]

我试过用

numbers[index].remove('')

但那只是给我一个列表索引超出范围错误

1 个答案:

答案 0 :(得分:0)

In [73]: L = [['3','4','5'],['',''],['6','7','8']]

In [74]: dels = []

In [75]: for i,sub in enumerate(L):
   ....:     if all(e=='' for e in sub):
   ....:         dels.append(i)
   ....:         

In [76]: for i in dels[::-1]:
   ....:     L.pop(i)
   ....:     

In [77]: L
Out[77]: [['3', '4', '5'], ['6', '7', '8']]