Last modified: Apr 18, 2023 By Alexander Williams
Remove None From List, List of List and Array In Python (Examples)
Remove None From List
Here are a few ways to remove None
values from a list in Python:
Using a List Comprehension
# Example of using a list comprehension to remove None values from a list
my_list = [1, None, 2, None, 3, None, 4]
new_list = [item for item in my_list if item is not None]
print(new_list)
Output:
[1, 2, 3, 4]
Using the filter() Function
# Example of using the filter() function to remove None values from a list
my_list = [1, None, 2, None, 3, None, 4]
new_list = list(filter(lambda x: x is not None, my_list))
print(new_list)
Output:
[1, 2, 3, 4]
Using a for Loop
# Example of using a for loop to remove None values from a list
my_list = [1, None, 2, None, 3, None, 4]
new_list = []
for item in my_list:
if item is not None:
new_list.append(item)
print(new_list)
Output:
[1, 2, 3, 4]
Using the list() Constructor
# Example of using the list() constructor to remove None values from a list
my_list = [1, None, 2, None, 3, None, 4]
new_list = list(filter(None, my_list))
print(new_list)
Output:
[1, 2, 3, 4]
Remove None From List of List
Here are ways to remove None
values from a list of list:
Using a Nested List Comprehension
# Example of using a nested list comprehension to remove None values from a list of lists
my_list = [[1, None, 2], [None, 3, None], [4, None, None]]
new_list = [[item for item in inner_list if item is not None] for inner_list in my_list]
print(new_list)
Output:
[[1, 2], [3], [4]]
Using a Filter Function with Lambda
# Example of using a filter function with lambda to remove None values from a list of lists
my_list = [[1, None, 2], [None, 3, None], [4, None, None]]
new_list = list(map(lambda x: list(filter(lambda y: y is not None, x)), my_list))
print(new_list)
Output:
[[1, 2], [3], [4]]
Using a List Comprehension with any()
# Example of using a list comprehension with any() to remove None values from a list of lists
my_list = [[1, None, 2], [None, 3, None], [4, None, None]]
new_list = [list(filter(lambda x: x is not None, inner_list)) for inner_list in my_list if any(inner_list)]
print(new_list)
Output:
[[1, 2], [3], [4]]
Remove None From Array
Here are a few ways to remove None
values from an array in Python:
Using a List Comprehension
# Example of using a list comprehension to remove None values from an array
import numpy as np
my_array = np.array([1, None, 2, None, 3, None, 4])
new_array = np.array([item for item in my_array if item is not None])
print(new_array)
Output:
[1 2 3 4]
Using the where() Function
# Example of using the where() function to remove None values from an array
import numpy as np
my_array = np.array([1, None, 2, None, 3, None, 4])
new_array = np.where(my_array != None)[0]
print(new_array)
Output:
[0 2 4 6]
Using the delete() Function
# Example of using the delete() function to remove None values from an array
import numpy as np
my_array = np.array([1, None, 2, None, 3, None, 4])
new_array = np.delete(my_array, np.where(my_array == None))
print(new_array)
Output:
[1 2 3 4]