How to remove null values from an array in Python

Preface

In this tutorial, we will learn some simple and easy methods to remove null or empty values from the array. It is really difficult when it comes to see null values in our array and python provides really useful functions and methods for you to easily remove the null values

remove(value) method is used to remove the item specified in value column from a given array of items. The method does not return any value but removes the given object from the list

For example:

fruits=[“orange”,”mango”,”apple”]

fruits.remove(“apple”)

output: [“orange”,”mango”]

Code

a=[1,"",2,"NULL",3];
for i in a:             #to loop through the array values
    if i == "":         #checks whether the value is ""
      a.remove("")      #remove the empty values
    elif i == "NULL":         #checks whether the value is null
      a.remove("NULL")         #removes the null value
print(a)

Output

[1,2,3]

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top