Python | Remove None values from list
Due to the upcoming of Machine Learning, the focus has now moved on handling the None values than ever before, the reason behind this is that it is the essential step of data preprocessing before it is fed into further techniques to perform. Hence, removal of None values in essential and knowledge of it is a must. Let’s discuss certain ways in which this is achieved.
Method #1 : Naive Method
In naive method, we iterate through the whole list and append all the filtered, non-None values into a new list, hence ready to be performed with subsequent operations.
# Python3 code to demonstrate # removing None values in list # using naive method # initializing list test_list = [1, None, 4, None, None, 5, 8, None] # printing original list print ("The original list is : " + str(test_list)) # using naive method # to remove None values in list res = [] for val in test_list: if val != None : res.append(val) # printing result print ("List after removal of None values : " + str(res)) |
The original list is : [1, None, 4, None, None, 5, 8, None] List after removal of None values : [1, 4, 5, 8]
Method #2 : Using list comprehension
The longer task of using the naive method and increasing line of codes can be done in a compact way using this method. We just check for True values and construct the new filtered list.
# Python3 code to demonstrate # removing None values in list # using list comprehension # initializing list test_list = [1, None, 4, None, None, 5, 8, None] # printing original list print ("The original list is : " + str(test_list)) # using list comprehension # to remove None values in list res = [i for i in test_list if i] # printing result print ("List after removal of None values : " + str(res)) |
The original list is : [1, None, 4, None, None, 5, 8, None] List after removal of None values : [1, 4, 5, 8]
Method #3 : Using filter()
filter function is the most concise and readable way to perform this particular task. It checks for any None value in list and removes them and form a filtered list without the None values.
# Python3 code to demonstrate # removing None values in list # using filter() # initializing list test_list = [1, None, 4, None, None, 5, 8, None] # printing original list print ("The original list is : " + str(test_list)) # using filter() # to remove None values in list res = list(filter(None, test_list)) # printing result print ("List after removal of None values : " + str(res)) |
The original list is : [1, None, 4, None, None, 5, 8, None] List after removal of None values : [1, 4, 5, 8]
Recommended Posts:
- Python | Remove all values from a list present in other list
- Python | Sort the values of first list using second list
- Python | Check if all the values in a list are less than a given value
- Python | Get values of particular key in list of dictionaries
- Python | Filter even values from a list
- Python | Get unique values from a list
- Python | Merge Python key values to list
- Python | Altering duplicate values from given list
- Python | Combining values from dictionary of list
- Python | Sort list containing alphanumeric values
- Python | Check if all the values in a list that are greater than a given value
- Python | Get Unique values from list of dictionary
- Python | Summation of dictionary list values
- Python | Find indices with None values in given list
- Python | Number of values greater than K in list
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



