Python | Remove trailing empty elements from given list
Given a list, the task is to remove trailing None values from last of the list. Let’s discuss a few methods to solve the given task.
Examples:
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]
Method #1: Using naive method
# 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)) |
chevron_right
filter_none
Output:
initial dictionary [1, 2, 3, 4, None, 76, None, None, None] resultant list [1, 2, 3, 4, None, 76]
Method #2: Using itertools.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)) |
chevron_right
filter_none
Output:
initial dictionary [1, 2, 3, 4, None, 76, None, None, None] resultant list [1, 2, 3, 4, None, 76]
Method #3: Using itertools.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)) |
chevron_right
filter_none
Output:
initial dictionary [1, 2, 3, 4, None, 76, None, None, None] resultant list [1, 2, 3, 4, None, 76]
Method #4: Using enumerate and list comprehension
# 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)) |
chevron_right
filter_none
Output:
initial dictionary [1, 2, 3, 4, None, 76, None, None, None] resultant list [1, 2, 3, 4, None, 76]
Recommended Posts:
- Python | Remove trailing/leading special characters from strings list
- Python | Remove empty tuples from a list
- Python | Remove and print every third from list until it becomes empty
- Python | Remove empty strings from list of strings
- Python | Remove last K elements of list
- Remove multiple elements from a list in Python
- Python | Remove unordered duplicate elements from a list
- Python | Remove elements of list that are repeated less than k times
- Python | First Non-Empty String in list
- Python | Check whether a list is empty or not
- Python | Initialize list with empty dictionaries
- Python | Splitting list on empty string
- Python | Remove all values from a list present in other list
- Python list remove()
- Python | Remove None values from list
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



