Home >>Python Programs >Remove false values from a list in Python
In this example, we will see a Python program through which we can remove the false values from a given list.
The values like False, None, 0 and "" that evaluate to False are considered as False values.
To remove these values from the list we will use the filter() method that will filter out the falsy values.
Program:
# Remove false values from a list in Python
def newlist(lst):
return list(filter(None, lst))
# main code
list1 = [10, 20, 0, 30, 0, None, ""]
list2 = [40, False, "Jerry", "", None]
list3 = [False, None, 0, "", "Abhi", 10, "Hi!"]
# printing original strings
print("list1: ", list1)
print("list2: ", list2)
print("list3: ", list3)
# removing falsy values and printing
print("newlist(list1): ", newlist(list1))
print("newlist(list2): ", newlist(list2))
print("newlist(list3): ", newlist(list3))