Python program to remove null values from a dictionary


Dictionaries are known as the collection data types. They store data in the form of key value pair. They are ordered and changeable i.e., they follow a particular sequence and are indexed. We can change the values for a key and therefore it is manipulative or changeable. Dictionaries does not support duplication of data. Each key can have multiple values associated with it but a single value cannot have more than one key. We can use dictionaries to perform numerous operations. The entire mechanism depends upon the stored values.

In this article, we will discuss the techniques that can be used to remove “null values” from a dictionary. Before we start with the main operation, we have to develop a firm understanding of value handling in dictionary. let’s quickly go through the overview of this article.

This article is divided into two sections −

  • The 1st section will focus on the concept of “null values” and its significance.

  • In the 2nd section, we will discuss the various methods through which we can remove these null values using pythonic codes.

Concept of Dictionary Values

Dictionary being a collection datatype stores data in the form of values. These values can be of any datatype but should be associated to an immutable key object. Let’s see the syntax for different types of values −

dict1 = {"key1": "value1", "key2": "value2"}

Here, each key has a single value but we can assign multiple values to a single key −

dict1 = {"key1": ("art", 3, 4.5), "key2": ("logic", 7, 5.5)}

As we can see values can be of any datatype. Now that we have understood the concept of values in a dictionary, let’s understand the logic of null values.

Concept Of Null Values

Null value is not an original pythonic concept, it is used in languages like Java or C. Though null values in python are vastly different. In python, null values are represented by “none” keywords. In other languages a null value acts as a pointer or a reference point but in python it is much bigger.

“none” keywords act as a first-class citizen in python. It is just not limited to a zero-value variable but its role extends till function operations. Whenever a function carries a “no return statement”, the none value is returned.

There are numerous advantages of using “none” values in python −

  • We can use none as a default parameter for recalling function multiple times.

  • We can also use it as a null value.

  • For declaring null variables.

  • Decoding the affect of null values in tracebacks errors.

  • We can use none to produce null objects for functions.

These are the advantages of “none” keyword in python. Sometimes removal of these kind of values become necessary and this is the reason we will discuss the different methods to pick up and discard none values.

Following are the methods to remove null values from the dictionary −

Using the Brute Approach

In this method, we will check all the values in the dictionary and pick up the one with “null values”. Let’s see its implementation −

Example

In the following program −

  • We created a dictionary with input data.

  • We created an empty dictionary to store the values that are not “none”.

  • We iterated through the dictionary and established a condition that filters the null values. At last, we printed the new dictionary.

dict1 = {"key1": 2, "key2": None, "key3": 5, "key4": "abc"}
dictrem = {}
for keys, values in dict1.items():
   if values is not None:
      dictrem[keys] = values
print(dictrem)

Output

{'key1': 2, 'key3': 5, 'key4': 'abc'} 

Using Dictionary Comprehension

This is a much better and optimal approach for discarding the null values. We will elegantly pass a single line command to filter these values. Let’s see the implementation −

Example

The following example helps us to understand the Above concept empirically.

dict1 = {"key1": 2, "key2": None, "key3": 5, "key4": "abc"}
dictrem = {keys:values for keys, values in dict1.items() if values is not None}
print(dictrem)

Output

{'key1': 2, 'key3': 5, 'key4': 'abc'} 

Example

dict1 = {"key1": 2, "key2": None, "key3": 5, "key4": "abc", "key5": 0}
dictrem = {keys:values for keys, values in dict1.items() if values is not None and values != 0}
print(dictrem)

Output

{'key1': 2, 'key3': 5, 'key4': 'abc'}

These are the basic methods that can be used to remove null values from dictionaries. The coder needs to segregate the values and this will be very helpful in case of huge input data. The second approach is a better way of handling null values.

Conclusion

In this article, we discussed the basic concepts of dictionaries and the meaning of keys and values. We understood the importance and significance of null values in python. At last, we discussed the different method to remove null values from a dictionary.

Updated on: 24-Apr-2023

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements