Python | Eliminar elementos vacíos finales de la lista dada

Dada una lista, la tarea es eliminar los valores finales de Ninguno del último de la lista. Analicemos algunos métodos para resolver la tarea dada.

Ejemplos:

Input: [1, 2, 3, 4, None, 76, None, None]
Output:  [1, 2, 3, 4, None, 76]

Input: [1, 2, None, None, None, None, None, 5]
Output:  [1, 2, None, None, None, None, None, 5]

Método #1: Usar un método ingenuo

# Python code to demonstrate
# to remove trailing None
# elements from lists
  
# initialising lists
ini_list = [1, 2, 3, 4, None, 76, None, None, None]
  
# printing initial dictionary
print ("initial dictionary", str(ini_list))
  
# code toremove trailing None values
# from lists
while not ini_list[-1]:
    ini_list.pop()
      
# printing result
print ("resultant list", str(ini_list))
Producción:

initial dictionary [1, 2, 3, 4, None, 76, None, None, None]
resultant list [1, 2, 3, 4, None, 76]

 
Método #2: Usaritertools.dropwhile()

# Python code to demonstrate
# to remove trailing None
# elements from lists
  
from itertools import dropwhile
# initialising lists
ini_list = [1, 2, 3, 4, None, 76, None, None, None]
  
# printing initial dictionary
print ("initial dictionary", str(ini_list))
  
# code toremove trailing None values
# from lists
res = list(reversed(tuple(dropwhile(lambda x: x is None, 
                                    reversed(ini_list)))))
      
# printing result
print ("resultant list", str(res))
Producción:

initial dictionary [1, 2, 3, 4, None, 76, None, None, None]
resultant list [1, 2, 3, 4, None, 76]

Método #3: Usaritertools.takewhile()

# Python code to demonstrate
# to remove trailing None
# elements from lists
  
from itertools import takewhile
  
# initialising lists
ini_list = [1, 2, 3, 4, None, 76, None, None, None]
  
# printing initial dictionary
print ("initial dictionary", str(ini_list))
  
# code toremove trailing None values
# from lists
res = ini_list[:-len(list(takewhile(lambda x: x == None,
                                  reversed(ini_list))))]
# printing result
print ("resultant list", str(res))
Producción:

initial dictionary [1, 2, 3, 4, None, 76, None, None, None]
resultant list [1, 2, 3, 4, None, 76]

 
Método n.° 4: usar la comprensión de enumeración y lista

# Python code to demonstrate
# to remove trailing None
# elements from lists
  
  
# initialising lists
ini_list = [1, 2, 3, 4, None, 76, None, None, None]
  
# printing initial dictionary
print ("initial dictionary", str(ini_list))
  
# code toremove trailing None values
# from lists
res = [x for n, x in enumerate(ini_list)
       if any(y is not None for y in ini_list[n:])]
  
# printing result
print ("resultant list", str(res))
Producción:

initial dictionary [1, 2, 3, 4, None, 76, None, None, None]
resultant list [1, 2, 3, 4, None, 76]

Publicación traducida automáticamente

Artículo escrito por garg_ak0109 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *