Remove falsy values from a list in Python

Python | Remove falsy values from a list: In this tutorial, we will learn how to remove falsy values from a Python list. Learn with the help of examples. By IncludeHelp Last updated : June 28, 2023

The falsy values are those values that evaluate to False such as False, None, 0, empty string (""), empty list ([]), empty dictionary ({}), empty tuple, and empty set.

Problem statement

Given a Python list, we have to remove the falsy values from it. Consider the below-given example with sample input/output values:

Example:

Input:
[10, 20, 0, 30, 0, None]

Output:
[10, 20, 30]

Input: 
[False, None, 0, "", "Hello", 10, "Hi!"]
    
Output: 
['Hello', 10, 'Hi!']

Solution 1: Remove falsy values from a list using filter() and None

Use the filter() method by passing None and sequence, you can filter the non-falsy values and convert the result into a new list using the list() method. In this way, you can get a list after removing (or, without) the falsy values. Consider the below code statement,

list(filter(None, list))

Here, we are implementing a python program to remove the falsy values from a string. To remove these values we are using filter() method, it will filter out the falsy values.

Example 1

# Remove falsy values from a list in Python

# Method will return a list
# without falsy values
def newlist(lst):
    return list(filter(None, lst))

# main code
list1 = [10, 20, 0, 30, 0, None]
list2 = [40, False, "Hello", "", None]
list3 = [False, None, 0, "", "Hello", 10, "Hi!"]

# printing original lists
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))

Output

list1:  [10, 20, 0, 30, 0, None]
list2:  [40, False, 'Hello', '', None]
list3:  [False, None, 0, '', 'Hello', 10, 'Hi!']
newlist(list1):  [10, 20, 30]
newlist(list2):  [40, 'Hello']
newlist(list3):  ['Hello', 10, 'Hi!']

Example 2

# Remove falsy values from a list in Python

# Method will return a list
# without falsy values
def newlist(lst):
    return list(filter(None, lst))

# main code
# list having empty string, dictionary, set also
list1 = [10, 20, 0, 30, 0, "", [], 10, {}, 20, ()]

# printing original list
print("list1: ", list1)

# removing falsy values and printing
print("newlist(list1): ", newlist(list1))

Output

list1:  [10, 20, 0, 30, 0, '', [], 10, {}, 20, ()]
newlist(list1):  [10, 20, 30, 10, 20]

Solution 2: Remove falsy values from a list using filter() and bool

You can also use bool as a parameter inside the filter() method to filter the truthy or falsy values. Consider the below code statement,

list(filter(bool, list))

Example

# Remove falsy values from a list in Python

# Method will return a list
# without falsy values
def newlist(lst):
    return list(filter(bool, lst))

# main code
# list having empty string, dictionary, set also
list1 = [10, 20, 0, 30, 0, "", [], 10, {}, 20, ()]

# printing original list
print("list1: ", list1)

# removing falsy values and printing
print("newlist(list1): ", newlist(list1))

Output

list1:  [10, 20, 0, 30, 0, '', [], 10, {}, 20, ()]
newlist(list1):  [10, 20, 30, 10, 20]

Solution 3: Remove falsy values from a list using list comprehension

By using Python list comprehension, you can filter the all falsy values from a given list to create a new list without falsy values. Consider the below code statement,

Example

# Remove falsy values from the list
# using list comprehension

# list
list1 = [10, 20, 0, 30, 0, "", [], 10, {}, 20, ()]

# printing original list
print("list1: ", list1)

# filtering falsy values, and
# creating a new list
result = [x for x in list1 if x]

# removing falsy values and printing
print("Result:", result)

Output

list1:  [10, 20, 0, 30, 0, '', [], 10, {}, 20, ()]
Result: [10, 20, 30, 10, 20]

Python List Programs »



Related Programs



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.