How to Remove null Values from the List in Python?

By Hardik Savani October 30, 2023 Category : Python

Hi,

Today our leading topic is python remove null values from list. I explained simply step by step remove null values from array python. We will use how to remove null values in python list. we will help you to give example of how to remove none values in list python. Alright, let’s dive into the steps.

There are a several ways to remove null value from list in python. we will use filter(), join() and remove() functions to delete empty string from list. so let's see below examples.

You can use these examples with python3 (Python 3) version.

let's see below simple example with output:

Example 1:

main.py

myList = ['', 'ItSolutionStuff.com', '', 'is', 'Best', '']
  
# Remove Empty Value from List
newList = list(filter(None, myList))
  
print(newList)

Output:

['ItSolutionStuff.com', 'is', 'Best']

Example 2:

main.py

myList = ['', 'ItSolutionStuff.com', '', 'is', 'Best', '']
  
# Remove Empty Value from List
newList = ' '.join(myList).split()
  
print(newList)

Output:

['ItSolutionStuff.com', 'is', 'Best']

Example 3:

main.py

myList = ['', 'ItSolutionStuff.com', ' ', 'is', 'Best', '']
  
# Remove Empty Value from List
while("" in myList) :
    myList.remove("")
  
print(myList)

Output:

['ItSolutionStuff.com', 'is', 'Best']

I hope it can help you...

Tags :
Shares