How to Remove All Zero/None/Null from Dictionary in Python?

How to Remove All Zero/None/Null from Dictionary in Python?

Dictionary is a prominent data structure in Python to store the key and value pair data. Every key in the dictionary has its associated value. That value can be anything legit data structure in python.

Sometimes we need to remove all the dictionary key-value pair having value equals to zero.

Program to remove all 0 from dictionary in Python

Method 1: Using for Loop

We can loop over the dictionary value and can put the check if the value is not equal to zero. In an earlier complete Python dictionary tutorial, we have seen how you can iterate over a complete Python dictionary.

dic_in = {'a': 2, 'b': 0, 'c': 4, 'd': 4, 'e': 0}
dic_out = {}
for x, y in dic_in.items():
    if y != 0:
        dic_out[x] = y

print(dic_out)

Output:

{'a': 2, 'c': 4, 'd': 4}

We are doing lot of labor work here. Can we optimize this code using some Python advance concepts?

Yes. We can do it.

Method 2: Using Python Compression Technique

There is also the easiest way to do this using the python compression technique.

Here is simple code spinet we can use it to filter out the the dictionary items having values zero.

{x:y for x,y in dic.items() if y!=0}

You can run the complete program here that removes all 0 from the dictionary in Python.

dic_in = {'a': 2, 'b': 0, 'c': 4, 'd': 4, 'e': 0}
dic_out = {x:y for x,y in dic_in.items() if y!=0}
print(dic_out)

Output:

{'a': 2, 'c': 4, 'd': 4}

This comprehension runs a loop over the key-value pair of a dictionary item. There is ‘if’ condition that excludes all the dictionary items having value equals to zero.

Similarly, you can write a program to remove all the dictionary elements have negative value by tweaking condition in the code.

Related Program: How to Set Default Dictionary Python Value?

Removing None Type Values from Dictionary

Many times, before processing a dictionary, project demands to remove all the dictionary entries having value None or Null.

If you don’t know Python has a None data type which is equivalent to Null data type from many of the other programming languages.

Here is the simple program that removes all the dictionary items having value None.

dic_in = {'a': 2, 'b': 0, 'c': None, 'd': 4, 'e': None}
dic_out = {x:y for x,y in dic_in.items() if y is not None}
print(dic_out)

Output:

{'a': 2, 'b': 0, 'd': 4}

Removing Zero and None Type Values from Dictionary

You can also combine the above two cases to remove all the entries having values zero or None type.

dic_in = {'a': 2, 'b': 0, 'c': 0, 'd': 4, 'e': None}
dic_out = {x:y for x,y in dic_in.items() if (y is not None and y!=0) }
print(dic_out)

Output:

{'a': 2, 'd': 4}

Here we are using logical ‘and’ operation.

Use Case

One of the best use cases, filtering all the students who scored marks more than 50.

Suppose you have a dictionary that maintains the student’s exam score data where the key is student’s id and value is student’s mark.

Note: Comprehension is a generic technique where you can put any if condition as per the requirement. It filters the dictionary item as per the condition.

It’s so easy. Right?

Python is known for its simplicity.

2 Comments

  1. dicSample = {'a': 2, 'b': 0, 'c': 4, 'd': 4, 'e': 0}
    dicSample2 = {x:y for x,y in dicSample.items() if y!=0}
    print (dicSample2)
    

    or

    dic = {'a': 2, 'b': 0, 'c': 4, 'd': 4, 'e': 0}
    dicSample2 = {x:y for x,y in dic.items() if y!=0}
    print (dicSample2)
    

Leave a Reply

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