Remove none from list Python
In this post, you will learn different ways to remove none from a list using the Python programming language.
A list is an arranged collection of elements. It is used to store collections of data. It can contain a list of various types of data objects with a comma separated and enclosed within a square bracket. It is mutable, which means we can change the order of elements. Individual elements can be replaced even after the list has been created. This is one of the leading reasons why lists are being widely used. Lists play an effective role in small projects as they consume more memory.
list1 = ["Welcome", 6, "Hello", 10]
Python remove none from list using naive method
In the given Python program, we have used the naïve approach in which the program iterates through the whole list, checks whether the values are none, if yes, ignores them, and appends all the filtered, non-None values into a new list.
# initializing list
x_list = [11, 21, None, None, 16, None, 8, None, 10]
# printing original list
print ("The original list: " + str(x_list))
# using naive method
# to remove None values in list
result = []
for val in x_list:
if val != None :
result.append(val)
print ("List without None values: " + str(result))
Output of the above code:
Original list: [11, 21, None, None, 16, None, 8, None, 10]
List without None values: [11, 21, 16, 8, 10]
Python remove none from list using list comprehension
List comprehension offers a shorter syntax when you want to create a new list based on the values of an existing list. Here, we have used this approach to create a new list by performing an operation on each item in the existing list, i.e., it returned a new list after removing the None values from the list.
# initializing list
x_list = [11, 21, None, None, 16, None, 8, None, 10]
# printing original list
print ("The original list: " + str(x_list))
# using list comprehension
# remove None values in list
result = [i for i in x_list if i is not None]
# printing result
print ("List without None values: " + str(result))
Output of the above code:
The original list: [11, 21, None, None, 16, None, 8, None, 10]
List without None values: [11, 21, 16, 8, 10]
Python remove none from list using filter() and lambda function
The filter(function, list) function of Python offers an elegant way to filter out all the elements of a list, for which the function returns true. This function will be applied to every element of the list.
Syntax-filter(function, list)
Here, the function is the expression applied to all elements of the list argument. In the given Python program, the filter() method takes the lambda function as the first argument to filter out the None values. It checks for any None values in the list and removes them. We use the list() class to convert the filter object to a list.
# initializing list
x_list = [11, None, 23, None, 31, 30, None, 89]
# printing original list
print ("The original list: " + str(x_list))
# using filter()
# to remove None values in list
result = list(filter(lambda item: item is not None, x_list))
# printing result
print ("List without None values: " + str(result))
Output of the above code:
The original list: [11, None, 23, None, 31, 30, None, 89]
List without None values: [11, 23, 31, 30, 89]
Python remove none from list using for loop and remove() method
To remove none from the list in Python, we iterate over the list elements and compare each element with the none value, if we find any none element, we will remove none element from the list using the python list remove() method.
Syntax-list.remove(element)
This method takes a single element as an argument and removes that element from the list.
# initializing list
x_list = [11, 21, None, None, 16, 8, None]
# printing original list
print ("The original list: " + str(x_list))
# Using for loop and remove method
for ele in x_list.copy():
if ele is None:
x_list.remove(ele)
# printing result
print ("List without None values: " + str(x_list))
Output of the above code:
The original list: [11, 21, None, None, 16, 8, None]
List without None values: [11, 21, 16, 8]
Related Articles
Python Convert String to UppercaseConvert string to int Python
Python program to reverse a string
Count consonants in a string Python
Python split strings by comma
Count vowels in a string Python
Convert string to lowercase Python
Web scraping Python BeautifulSoup
Check if two strings are anagrams Python
Python program to add two numbers
Print new line python
Python iterate list with index
Convert List to Dataframe Python
numpy random choice
Dictionary inside list python
Strip function in Python
Python raise keyword
Check if two strings are anagrams Python
Python program to map two lists into a dictionary