Open In App

Python – Remove None values from list without removing 0 value

Last Updated : 20 Dec, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Removing None values from a list in Python is a common task when cleaning or processing data. This process helps to filter out unwanted None elements, leaving only the meaningful values in the list for further use. There can be multiple methods to remove None values from a Python list without removing 0.

Using List Comprehension

List comprehension in Python allows us to filter out None values from a list. It’s a fast way to create a new list with only the desired elements.

Example:

Python
a = [1, None, 3, None, 5, 0]

# create a new list `b` that excludes all `None` 
b = [x for x in a if x is not None] 
print(b)

Output
[1, 3, 5, 0]

Explanation:

  • It filters out None values from list a.
  • result is stored in list b.

Let’s discuss more method to remove none values from list.

Using filter()

filter() function allows us to remove None values by applying a condition to each element of the list. It creates a new list containing only the elements that meet the specified condition.

Example:

Python
a = [1, None, 3, 0, None, 5]

#remove `None` values from the list
#filter function applies the lambda function to each element of `a`
b = list(filter(lambda x: x is not None, a)) 
print(b)

Output
[1, 3, 5]

Explanation:

  • filter() with lambda filters out None values from list a.
  • Result is stored in list b, containing only the non-None elements.

Using itertools.filterfalse()

itertools.filterfalse() filters out None values by applying an inverted condition, keeping only elements that are not None. This approach provides an alternative to filter(), with the same O(n) time complexity for iterating through the list.

Example:

Python
import itertools

# Define a list containing integers and `None` values.
a = [1, None, 3, None, 0 , 5]

#filter out elements that satisfy the condition `x is None`
# lambda function `lambda x: x is None` 
#checks if the element `x` is `None`
b = list(itertools.filterfalse(lambda x: x is None, a))
print(b)

Output
[1, 3, 0, 5]

Explanation:

  • This code filters out None values from list a.
  • Result is stored in list b.

Using map()

map() function is applied to each item in the list, which allows for flexible transformations. In this approach, None values are first replaced with None and then removed using a list comprehension, resulting in a clean list.

Example:

Python
a = [1, None, 3, 0, None, 5]

# For `None` elements, the lambda explicitly returns `None`.
b = list(map(lambda x: x if x is not None else None, a)) 
b = [val for val in b if val is not None] 
print(b)

Output
[1, 3, 0, 5]

Explanation:

  • It applies lambda function to keep or replace None values.
  • list comprehension removes all None values from the result.

Using del keyword

del keyword allows us to remove None values directly from a list without creating a new one. This method modifies the list in place, making it efficient in terms of memory usage.

Example:

Python
a = [1, None, 3, 0, None, 5]

# Initialize a counter variable `i` to iterate through the list.
i = 0

# Use a `while` loop to iterate over the list.
while i < len(a):
    if a[i] is None:
        del a[i]
    else:
        i += 1
print(a)

Output
[1, 3, 0, 5]

Explanation:

  • This removes None values from the list a in place.
  • List is directly modified, keeping only non-None values.


Practice Tags :

Similar Reads