How to Remove None Values from a List in Python?

In this tutorial, I will explain how to remove None values from a list in Python. As a data scientist working with real-world datasets as a part of my project, I often encounter lists containing None values that need to be cleaned before further processing. I will share various methods to effectively remove None values while preserving other elements in the list.

Remove None Values from a List in Python

Let’s consider a scenario where we have a list of customer ages from a US-based company. However, some customers didn’t provide their age, resulting in None values in the list. Our goal is to remove these None values while keeping the valid age entries intact.

To remove None values from a list in Python, you can use list comprehension like this: cleaned_list = [item for item in original_list if item is not None]. For example, if original_list = [1, None, 2, None, 3] then cleaned_list will be [1, 2, 3].

ages = [28, None, 42, None, 31, 56, None, 37]

Read How to Find the Largest Number in a List Using Python?

Method 1: Use List Comprehension

One of the most concise and readable ways to remove None values from a list by using list comprehension in Python. It allows us to create a new list containing only the desired elements. Here’s how we can achieve this:

ages_clean = [age for age in ages if age is not None]
print(ages_clean)

Output:

[28, 42, 31, 56, 37]

You can refer to the below screenshot to see the output:

Remove None Values from a List in Python

In this example, we create a new list ages_clean by iterating over each element age in the original ages list. The condition if age is not None filters out the None values, keeping only the valid ages in the new list.

Check out How to Divide Each Element in a List by a Number in Python?

Method 2: Use the filter() Function

Another effective approach is to use the built-in filter() function in Python. It takes a function and an iterable as arguments and returns an iterator with elements for which the function returns True. Let’s see how we can use filter() to remove None values:

ages_clean = list(filter(lambda x: x is not None, ages))
print(ages_clean)

Output:

[28, 42, 31, 56, 37]

You can refer to the below screenshot to see the output:

How to Remove None Values from a List in Python

Here, we pass a lambda function lambda x: x is not None to filter(), which checks each element x in the ages list and keeps only the non-None values. Finally, we convert the iterator returned by filter() back to a list using the list() function.

Read How to Find the Closest Value in a List Using Python?

Method 3: Define a Custom Function

For more flexibility and reusability, we can define a custom function in Python to remove None values from a list. This approach is particularly useful when working with multiple lists or when the removal logic is more complex.

def remove_none(lst):
    return [item for item in lst if item is not None]

ages_clean = remove_none(ages)
print(ages_clean)

Output:

[28, 42, 31, 56, 37]

You can refer to the below screenshot to see the output:

Remove None Values from a List in Python define a custom function

In this example, we define a function called remove_none() that takes a list lst as input. Inside the function, we use list comprehension to create a new list containing only the non-None values. Finally, we call the remove_none() function with our ages list to obtain the cleaned version.

Check out How to Sort a List in Python Without Using the sort() Function?

Example

Let’s consider a more realistic example where we have a list of customer names and their corresponding state. However, some customers didn’t provide their state, resulting in None values in the list.

customer_data = [
    {'name': 'John Doe', 'state': 'California'},
    {'name': 'Jane Smith', 'state': None},
    {'name': 'Michael Johnson', 'state': 'Texas'},
    {'name': 'Emily Davis', 'state': None},
    {'name': 'David Wilson', 'state': 'New York'}
]

To remove the customers with missing state information, we can use a list comprehension with a condition that checks for the presence of the ‘state’ key and its value:

customer_data_clean = [customer for customer in customer_data if customer['state'] is not None]

The resulting customer_data_clean list will contain only the customers with valid state information:

[
    {'name': 'John Doe', 'state': 'California'},
    {'name': 'Michael Johnson', 'state': 'Texas'},
    {'name': 'David Wilson', 'state': 'New York'}
]

Read How to Check if Any Element in a List is Present in Another List using Python?

Handle None Values in Nested Lists

In some cases, you might encounter None values within nested lists. To remove None values from nested lists, you can use a recursive approach. Here’s an example:

def remove_none_nested(lst):
    return [remove_none_nested(item) if isinstance(item, list) else item for item in lst if item is not None]

nested_list = [1, [2, None, 4], None, [5, [None, 7]], 8]
cleaned_list = remove_none_nested(nested_list)

In this example, the remove_none_nested() function recursively calls itself whenever it encounters a nested list. It applies the removal logic to each sublist until all None values are eliminated.

Output:

[1, [2, 4], [5, [7]], 8]

Check out How to Capitalize the First Letter of Every Word in a List using Python?

Conclusion

In this tutorial, I helped you to understand how to remove None values from a list in Python. I explained three methods to accomplish this task such as using list comprehension, using filter() function, and defining a custom function. I also discussed a real-time example and handling None values in nested lists.

You may read:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.