Removing trailing empty elements in a list
list, python
Solution
If you want to get rid of only `None` values and leave zeros or other falsy values, you could do:
while my_list and my_list[-1] is None:
my_list.pop()
To remove all falsy values (zeros, empty strings, empty lists, etc.) you can do:
my_list = [1, 2, 3, None, 4, None, None]
while not my_list[-1]:
my_list.pop()
print(my_list)
# [1, 2, 3, None, 4]
Problem
Is there an elegant pythonic way of removing trailing empty elements from a list? A sort of `list.rstrip(None)`. So ``` [1, 2, 3, None, 4, None, None] ``` should result in ``` [1, 2, 3, None, 4] ``` I guess this could be generalized to removing trailing elements of any particular value. If possible, I would like to have this done as a single line (readable) expression