Python has a string method called rstrip():
>>> s = "hello world!!!"
>>> s.rstrip("!")
'hello world'
I want to implement similar functionality for a Python list. That is, I want to remove all instances of a given value from the end of a list. In this case, the value is None.
Here's some starting examples:
[1, 2, 3, None]
[1, 2, 3, None, None, None]
[1, 2, 3, None, 4, 5]
[1, 2, 3, None, None, 4, 5, None, None]
I want the end results to be:
[1, 2, 3]
[1, 2, 3]
[1, 2, 3, None, 4, 5]
[1, 2, 3, None, None, 4, 5]
Here's my solution so far:
while l[-1] is None:
l.pop()