Python | Filter the negative values from given dictionary

Given a dictionary, the task is to filter all the negative values from given dictionary. Let’s discuss few methods to do this task.

Method #1: Using dict comprehension

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python code to demonstrate
# return the filtered dictionary
# on certain criteria
  
from six import iteritems
# Initialising dictionary
ini_dict = {'a':1, 'b':-2, 'c':-3, 'd':7, 'e':0}
  
# printing initial dictionary
print ("initial lists", str(ini_dict))
  
# filter dictionary such that no value is greater than 0
result = dict((k, v) for k, v in ini_dict.items() if v >= 0)
  
print("resultant dictionary : ", str(result))

chevron_right


Output:

initial lists {'a': 1, 'c': -3, 'd': 7, 'b': -2, 'e': 0}
resultant dictionary :  {'a': 1, 'd': 7, 'e': 0}

 
Method #2: Using lambda and filter

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python code to demonstrate
# return the filtered dictionary
# on certain criteria
  
from six import iteritems
# Initialising dictionary
ini_dict = {'a':1, 'b':-2, 'c':-3, 'd':7, 'e':0}
  
# printing initial dictionary
print ("initial lists", str(ini_dict))
  
# filter dictionary such that no value is greater than 0
result = dict(filter(lambda x: x[1] >= 0.0, ini_dict.items()))
result = dict(result)
  
print("resultant dictionary : ", str(result))

chevron_right


Output:

initial lists {'c': -3, 'd': 7, 'e': 0, 'a': 1, 'b': -2}
resultant dictionary :  {'e': 0, 'a': 1, 'd': 7}


My Personal Notes arrow_drop_up

Programming freaktech Enthusiast and have interest in learning new upcoming technologies

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.