| by Arround The Web | No comments

Remove None from the List Python

Lists are used to store data of various types, and this data can be directly used for data analytics or for data processing purposes. The “None” in the list can cause severe ambiguities in the result of those operations, which is not what you want, do you? Well, this guide will use different methods and approaches to remove all of the “None” values from the list.

Listed below are all the different solutions that can be used to remove the None from the list in Python:

Solution 1: Using the remove() Method With the __contains__() Method

Whenever the user wants to remove an element, the very first method that comes to mind is the remove() method. However, the remove method cannot be used to remove all/multiple None values from the list. Because the remove() method only removes the first instance of the element from the list.

To demonstrate this, take the following code:

listVar  = [69,22,65,None, None, 123,6789,None]

listVar.remove(None)
print(listVar)

 

Executing this code will produce the following result on the terminal of your machine:

The output confirms that only the first instance of “None” was removed. To remove all the instances of the “None” from the list, use the while loop along with the __contains__() method:

listVar  = [69,22,65,None, None, 123,6789,None]

while listVar.__contains__(None):
    listVar.remove(None)
print(listVar)

 

In this code snippet, the while loop keeps running until there are no more instances of “None” left in the list. When this code is executed, it produces the following result on the terminal:

The output confirms that there are no more instances of “None” found in the list, meaning all of them have been removed successfully.

Solution 2: Using List Comprehension With If-Condition

Another way of removing the “None” from the list is by using the list comprehension to go through every element, comparing it with “None” through the if-condition, and placing the non-None values in a separate list.

To remove None from the list using this method, take the following code snippet:

listVar  = [69,22,65,None, None, 123,6789,None]
newList= []
for x in listVar:
    if x is not None:
        newList.append(x)
       
print("Original List: ",listVar)
print("New List: ",newList)

 

When this code is executed, it will produce the following outcome on the terminal of your machine:

The output confirms that there are no “None” values present in the new list.

Solution 3: Using the Filter() Method to Remove None Values From List

The filter() method can also be used to remove “None” from a list, but using it normally can cause some issues. The filter() method considers the value “0” as “None”, and removes that from the list as well. To demonstrate this, take the following piece of code:

listVar  = [69,0,22,65,0,None, None, 123,6789,None]

res = list(filter(None, listVar))
print("Original List: ", listVar)
print("List After Filter: ",res)

 

When this code is executed, it will produce the following result on your terminal:

From the output, it can be clearly observed that the filter() method has also removed the value “0” from the list.

The correct way to use the filter() method to remove all the “None” values from the list without interrupting other values of the list is to use a “Lambda Statement” within the first argument:

listVar  = [69,0,22,65,0,None, None, 123,6789,None]

res = list(filter(lambda elem: elem is not None, listVar))
print("Original List: ", listVar)
print("List After Filter: ",res)

 

This time around when the code is executed, it will produce the following output on the terminal:

As you can see, you have successfully removed all the “None” values from the list successfully using the filter() method.

Conclusion

The user can remove all the “None” values from a Python list by using the remove() method along with the __contains__() method, list comprehension with if-condition, or by using the filter() method with lambda statement. All of these three methods have been thoroughly elaborated in this post with pictorial representation.

Share Button

Source: linuxhint.com

Leave a Reply