Eliminar todas las listas anidadas vacías

Cómo a partir de esta lista:

list = [ [], ['', 'subitem'], [[]], 'item', [ 'item', 'item', [''], [] ], [] ] 

Puedo conseguir esto:

 list = [ ['subitem'], 'item', [ 'item', 'item' ] ] 

¿Cómo elimino recursivamente todas las listas anidadas vacías, cadenas cero y listas con cadenas cero anidadas?

Una sola línea:

 def remove_empty(l): return tuple(filter(lambda x:not isinstance(x, (str, list, tuple)) or x, (remove_empty(x) if isinstance(x, (tuple, list)) else x for x in l))) 

Recursion:

 def remove_lst(lst): if not isinstance(lst, list): return lst else: return [x for x in map(remove_lst, lst) if (x != [] and x != '')] 
 def purify(l): for (i, sl) in enumerate(l): if type(sl) == list: l[i] = purify(sl) return [i for i in l if i != [] and i != ''] l1 = [ [], ['', 'subitem'], [[]], 'item', [ 'item', 'item', [''], [] ], [] ] print purify(l1) 

Huellas dactilares:

 [['subitem'], 'item', ['item', 'item']] 

Tal vez algo como esto?

 #!/usr/local/cpython-3.3/bin/python '''Demonstrate a way of removing empty lists and empty strings from a nested list''' import pprint def remove_empty_recursive(input_obj): '''Remove empty lists and strings - worker function''' if isinstance(input_obj, str): if input_obj: # it's a nonempty string, pass it through return input_obj else: # string is empty, delete it return None elif isinstance(input_obj, list): if input_obj: new_obj = [] for element in input_obj: subresult = remove_empty_recursive(element) if subresult is None: pass else: new_obj.append(subresult) if new_obj: return new_obj else: return None else: return None def remove_empty(list_): '''Remove empty lists and strings - user callable portion''' result = remove_empty_recursive(list_) return result def for_comparison(): '''Show what the OP wanted, for comparison's sake''' list_ = [ ['subitem'], 'item', [ 'item', 'item' ] ] return pprint.pformat(list_) def main(): '''Main function''' list_ = [ [], ['', 'subitem'], [[]], 'item', [ 'item', 'item', [''], [] ], [] ] result = remove_empty(list_) print('actual result:') pprint.pprint(result) print('desired result:') print(for_comparison()) main() 

Si eso no es todo, es posible que desee compartir un ejemplo más detallado que falla.