Python

Remove None from the List Python

In python, when a function returns nothing, it indirectly returns ‘None’. Due to the forthcoming ML (Machine Learning), our focus is now on understanding the None values. The goal behind this is that it is the crucial phase of data preprocessing. Hence, elimination of None values is crucial, so you must know how important it is. Let’s discuss certain techniques in which this is achieved. To replace none in python, we use different techniques such as DataFrame, fillna, or Series. No keyword in python declares the null objects and variables. In python, none refers to the class ‘NoneType’.

We can allot None to many variables, and they all point toward a similar object. The interesting fact about none is that we can’t consider false as any. None is a blank string or a 0. Let’s demonstrate it with the help of examples. We use the Spyder compiler or different strategies to explain how python removes null values from the list.

Example 1

In our first illustration, we use a simple approach to explain how to remove none from the python list. We have created a new list, added non-None or none elements, and then traversed through the entire list. Let’s check how it works. To run your code, the very first thing you have to do is to launch Spyder IDE. So, from the Windows PC search bar, type ‘Spyder’ and then click open. Use keyboard shortcut ‘Ctrl+Shift+N’ to create a new file or move to the File menu. After creating a new file, write a python code to elaborate on removing none from the python list.

In this method, we first initialize our list and add none or non-None elements to it. Then we have used a print function that prints all the none or non-None items present in our new list. Then we have used our basic method to remove none values from the list. To check the None element, we use the if statement. If the elements are None in the list, it stores the element in the ‘result’; otherwise, it calls the append function. At last, we use a print function that displays the resultant output on the console screen.

my_list = [2, None, 3, None, None, 8, None, 9]

print ("My list is : " + str(my_list))

result = []

for val in my_list:

if val != None :

res.append(val)

print ("List after removing None values : "str(result))

Once you successfully write the python code, move to the File menu and save your code file with the ‘.py’ extension. In our illustration, the file name is ‘RemoveNone.py’. You can specify any file name in your illustration.

Use the “F9” key to run your program file or verify the output of a python code on your console screen.

Example 2

The inappropriate thing about using the first method is that it is very time-consuming. Too many lines of code are a waste of time, so here we do the same thing but in a compressed way. We looked for the non-None values and made a new list.

Let’s check how we do this in a very precise way. Open the Spyder compiler in Windows 10 and choose a new blank file or use the same file. We used the same python code file “RemoveNone.py” and made changes to it. At first, we initialize and print the list just like we did in the above code. Then we use a syntax of a list comprehension to eliminate none values from the list, and after that, we use a print function that prints the new filtered list having non-None values in it.

my_list = [2, None, 3, None, None, 8, None, 9]

print ("My list is : " + str(my_list))

result = [i for i in my_list if i]

print ("List after removing None values : "str(result))

Save the program file and run the code to check the output on the console screen.

Conclusion

This tutorial is all about how to remove None from the python list. Other than the above methods, you can also use the filter() function to remove None from the python list. I believe now you can easily implement both illustrations.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content