Python program to remove null value from a list

This article discusses how to remove None values from a Python list. In Python, None represents the absence of a value and is equivalent to "null" in other programming languages.

Understanding None in Python

None is a keyword in Python that represents an empty or missing value. It is not the same as 0, False, or an empty string ?

# None is unique
print(None == 0)        # False
print(None == False)    # False
print(None == "")       # False

# Only None equals None
print(None is None)     # True
False
False
False
True

Why Remove None Values?

None values in datasets can affect performance and accuracy of data processing operations. Removing them ensures cleaner data and prevents errors during analysis.

Method 1: Using filter()

The filter() function removes falsy values including None, empty strings, and zero ?

my_list = ['hi', 'Joy', None, 'whats', '', 'up', None]
print("Original list:", my_list)

filtered_list = list(filter(None, my_list))
print("After filter():", filtered_list)
Original list: ['hi', 'Joy', None, 'whats', '', 'up', None]
After filter(): ['hi', 'Joy', 'whats', 'up']

Method 2: Using For Loop

A simple loop checks each element and appends non-None values to a new list ?

# List with None values
my_list = [4, 5, True, False, None, 'List', None]

# Create new list without None
new_list = []
for element in my_list:
    if element is not None:
        new_list.append(element)

print("Original list:", my_list)
print("Without None:", new_list)
Original list: [4, 5, True, False, None, 'List', None]
Without None: [4, 5, True, False, 'List']

Method 3: Using List Comprehension

List comprehension provides a concise way to filter out None values ?

# List with None values
my_list = [0, 1, True, None, False, None]

# Using list comprehension
new_list = [item for item in my_list if item is not None]

print("Original list:", my_list)
print("Without None:", new_list)
Original list: [0, 1, True, None, False, None]
Without None: [0, 1, True, False]

Method 4: Using join() and split()

This method works specifically for removing empty strings from a list ?

my_list = ['', 'Salad', 'is', '', 'Good', '']
print("Original list:", my_list)

# Remove empty strings using join and split
new_list = ' '.join(my_list).split()
print("After join/split:", new_list)
Original list: ['', 'Salad', 'is', '', 'Good', '']
After join/split: ['Salad', 'is', 'Good']

Comparison of Methods

Method Removes None Removes Empty Strings Best For
filter() Yes Yes Removing all falsy values
For loop Yes No Custom filtering logic
List comprehension Yes No Readable, Pythonic code
join/split No Yes String lists only

Conclusion

Use filter() to remove all falsy values, list comprehension for readable None-specific filtering, or a for loop when you need custom logic. Choose the method that best fits your specific use case.

Updated on: 2026-03-27T01:35:47+05:30

12K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements