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]
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…