- remove None value from a list without removing the 0 value
- 11 Answers 11
- How to remove none from list python (5 Ways)
- What is none in python?
- Why should we remove none from list Python?
- How to remove none from list Python
- Method 1- Remove none from list using filter() method
- Method 2- Naive Method
- Method 3- Using For loop + list remove()
- Method 4- Using List Comprehension
- Method 5- Using Filter() + lambda function
- Conclusion
remove None value from a list without removing the 0 value
Because I’m calculating percentile of the data and the 0 make a lot of difference. How to remove the None value from a list without removing 0 value?
11 Answers 11
>>> L = [0, 23, 234, 89, None, 0, 35, 9] >>> [x for x in L if x is not None] [0, 23, 234, 89, 0, 35, 9]
Just for fun, here’s how you can adapt filter to do this without using a lambda , (I wouldn’t recommend this code — it’s just for scientific purposes)
>>> from operator import is_not >>> from functools import partial >>> L = [0, 23, 234, 89, None, 0, 35, 9] >>> list(filter(partial(is_not, None), L)) [0, 23, 234, 89, 0, 35, 9]
The less elegant filter version: filter(lambda x: x is not None, L) — You could get rid of the lambda using partial and operator.is_not I think, but it’s probably not worth it since the list-comp is so much cleaner.
@mgilson Oh wow I didn’t even know is_not existed! I thought it was only is_ , I’m gonna add that in just for fun
@jamylak — Yeah. It actually bothers me that is_not exists and not_in doesn’t exist. I actually think that not_in should be turned into a magic method __not_contains__ . see a question I asked a while back and a comment I made to an answerer . and still don’t feel like it is resolved.
@mgilson I think under that same assumption I just assumed it didn’t exist. I guess you can just use filterfalse or something depending on the use case
@jamylak — Yeah. My main problem is that x > y does not imply not x
A list comprehension is likely the cleanest way:
>>> L = [0, 23, 234, 89, None, 0, 35, 9 >>> [x for x in L if x is not None] [0, 23, 234, 89, 0, 35, 9]
There is also a functional programming approach but it is more involved:
>>> from operator import is_not >>> from functools import partial >>> L = [0, 23, 234, 89, None, 0, 35, 9] >>> list(filter(partial(is_not, None), L)) [0, 23, 234, 89, 0, 35, 9]
@DrMcCleod The expression x != y internally calls x.__ne__(y) where the ne stands for «not equal». So, None.__ne__ is a bound method that returns True when called with any value other than None. For example, bm = None.__ne__ called with bm(10) returns NotImplemented which as true value, and bm(None) returns False.
That NotImplemented is true in boolean context is really bizarre. See Make NotImplemented unusable in boolean context Python issue.
Using list comprehension this can be done as follows:
l = [i for i in my_list if i is not None]
For Python 2.7 (See Raymond’s answer, for Python 3 equivalent):
Wanting to know whether something «is not None» is so common in python (and other OO languages), that in my Common.py (which I import to each module with «from Common import *»), I include these lines:
def exists(it): return (it is not None)
Then to remove None elements from a list, simply do:
I find this easier to read, than the corresponding list comprehension (which Raymond shows, as his Python 2 version).
I would prefer Raymonds solution for Python 3, and then the list comprehension for Python 2. But if I did have to go this route, I would rather partial(is_not, None) than this solution. I believe this will be slower (although thats not too important). But with a couple of imports of python modules, no need for a custom defined function in this case
The custom defined function isn’t merely for this case. I wouldn’t have defined a function if it were! I’m saying that I find it more readable, in many situations, to say «if exists(something)», instead of saying «if something is not None». Its closer to how I think, and avoids the «double-negative» of saying «not None».
I see what you mean about avoiding the double negative, actually in the definition of exists return (it is not None) is a clear way to define it. (maybe I’d remove the brackets but that’s just a small thing anyway)
@jamylak answer is quite nice, however if you don’t want to import a couple of modules just to do this simple task, write your own lambda in-place:
>>> L = [0, 23, 234, 89, None, 0, 35, 9] >>> filter(lambda v: v is not None, L) [0, 23, 234, 89, 0, 35, 9]
Iteration vs Space, usage could be an issue. In different situations profiling may show either to be «faster» and/or «less memory» intensive.
# first >>> L = [0, 23, 234, 89, None, 0, 35, 9, . ] >>> [x for x in L if x is not None] [0, 23, 234, 89, 0, 35, 9, . ] # second >>> L = [0, 23, 234, 89, None, 0, 35, 9] >>> for i in range(L.count(None)): L.remove(None) [0, 23, 234, 89, 0, 35, 9, . ]
The first approach (as also suggested by @jamylak, @Raymond Hettinger, and @Dipto) creates a duplicate list in memory, which could be costly of memory for a large list with few None entries.
The second approach goes through the list once, and then again each time until a None is reached. This could be less memory intensive, and the list will get smaller as it goes. The decrease in list size could have a speed up for lots of None entries in the front, but the worst case would be if lots of None entries were in the back.
The second approach would likely always be slower than the first approach. That does not make it an invalid consideration.
Parallelization and in-place techniques are other approaches, but each have their own complications in Python. Knowing the data and the runtime use-cases, as well profiling the program are where to start for intensive operations or large data.
Choosing either approach will probably not matter in common situations. It becomes more of a preference of notation. In fact, in those uncommon circumstances, numpy (example if L is numpy.array: L = L[L != numpy.array(None) (from here)) or cython may be worthwhile alternatives instead of attempting to micromanage Python optimizations.
How to remove none from list python (5 Ways)
In this article, we are solving the problem of how to remove none from list python. This type of problem in Python can be solved in many ways, we will be looking in 5 ways to remove none from list in python.
What is none in python?
None is a keyword in Python used to define a null value or no value at all. It is not the same as an empty string or zero, or false value, it is the data type of the class NoneType in Python.
We can declare none variable as –
a = None print("Value of a is", a) print("Type of a is", type(a))
Value of a is None Type of a is
Why should we remove none from list Python?
When analyzing a large set of data, it is likely that we come across with none values or missing values. To make sure data is clean and tidy for data analysis and data representation removing null values from list data in python is important.
Since null values affect the performance and accuracy of machine learning algorithms it is necessary to handle these none values from data sets before applying the machine learning algorithm. Removing such unwanted null values, and corrupting data before processing the algorithm is called data preprocessing.
How to remove none from list Python
The 5 Ways to get rid of none in Python are as follows-
Method 1- Remove none from list using filter() method
The easiest way to remove none from list in Python is by using the list filter() method. The list filter() method takes two parameters as function and iterator. To remove none values from the list we provide none as the function to filter() method and the list which contains none values.
None – To eliminate none values we provide none as the function in the filter method
Iterator – An iterator like list, set, or tuple.
Python Code:
# List with none values sample_list = [1,2, True, None, False, None, 'Python', None] # Using filter() method to filter None values filtered_list = list(filter(None, sample_list)) print("List with None values: ", sample_list) print("List without None values", filtered_list)
List with None values: [1, 2, True, None, False, None, 'Python', None] List without None values [1, 2, True, 'Python']
Method 2- Naive Method
We can also remove none from list in python using a for loop and check whether the values are none if yes ignore it and the rest of the values can be appended to another list.
Python Code:
# List with none values sample_list = [1,2, True, None, False, None, 'Python', None] # initialize filtered list filtered_list = [] # Using for loop for ele in sample_list: if ele != None: filtered_list.append(ele) print("List with None values: ", sample_list) print("List without None values", filtered_list)
List with None values: [1, 2, True, None, False, None, 'Python', None] List without None values [1, 2, True, 'Python']
Method 3- Using For loop + list remove()
To remove none from list python we will iterate over the list elements and compare each element with none if we find any none element we will remove none element from the list using the python list remove() method.
The list remove() method takes a single lament as an argument and removes that element from the list.
Python Code:
# List with none values sample_list = [1,2, True, None, False, None, 'Python', None] #Printing define list print("List with None values:", sample_list) # Using for loop and remove method for ele in sample_list: if ele == None: sample_list.remove(ele) # print list after removing none print("List without None values:", sample_list)
List with None values: [1, 2, True, None, False, None, 'Python', None] List without None values [1, 2, True, 'Python']
Method 4- Using List Comprehension
To remove none values from list we also use list comprehension. In Python, list comprehension is a way to create a new list from existing other iterables like lists, sets, and tuples. We can also say that list comprehension is a shorter version of for loop. To know about list comprehension you can visit this.
Python Code:
# List with none values sample_list = [1,2, True, None, False, None, 'Python', None] # Using List comprehension to remove none values filtered_list = [ ele for ele in sample_list if ele is not None ] print("List with None values: ", sample_list) print("List without None values", filtered_list)
List with None values: [1, 2, True, None, False, None, 'Python', None] List without None values [1, 2, True, 'Python']
Method 5- Using Filter() + lambda function
In python, the lambda function is an anonymous function that is without any name. It takes any number of arguments but can only perform one expression.
As we discuss above filter() method takes a function as input, and since lambda is also one kind of method in python, hence we provide a lambda function and an iterator as input arguments to the filter method.
Example - filter(lambda_function, iterator)
Python code:
# List with none values sample_list = [1,2, True, None, False, None, 'Python', None] # Using filter() method + lambda filtered_list = list(filter(lambda ele:ele is not None, sample_list)) print("List with None values: ", sample_list) print("List without None values", filtered_list)
List with None values: [1, 2, True, None, False, None, 'Python', None] List without None values [1, 2, True, 'Python']
Conclusion
Hence we have seen how to remove none from list python in 5 different ways. We can remove none from list python by using filter(), naive method, for loop + remove(), using list comprehension, and filter() + lambda function.
I am Passionate Computer Engineer. Writing articles about programming problems and concepts allows me to follow my passion for programming and helping others.