Skip to content
Related Articles
Open in App
Not now

Related Articles

Python | Filter the negative values from given dictionary

Improve Article
Save Article
  • Last Updated : 23 Jan, 2023
Improve Article
Save Article

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 

Python3




# Python code to demonstrate
# return the filtered dictionary
# on certain criteria
 
# 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))

Output

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

Time Complexity : O(N)
Auxiliary Space : O(N)

  Method #2: Using lambda and filter 

Python3




# Python code to demonstrate
# return the filtered dictionary
# on certain criteria
# 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))

Output

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

Method #3 : Using find() method

Python3




# Python code to demonstrate
# return the filtered dictionary
# on certain criteria
 
# 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
res = dict()
for i in ini_dict:
    if(str(ini_dict[i]).find("-") != 0):
        res[i] = ini_dict[i]
 
 
print("resultant dictionary : ", str(res))

Output

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

Method #4 :  Using map() and a custom function

Here is an example of using map() and a custom function to filter negative values from a dictionary:

Python3




#If the value is negative, it returns None.
def filter_negative(item):
  key, value = item
  if value < 0:
    return
  return (key, value)
 
#Initialize the dictionary
ini_dict = {'a': 1, 'b': -2, 'c': -3, 'd': 7, 'e': 0}
 
#Use map to apply the filter_negative function to each item in the dictionary and return a map object
#Use filter to remove the None values from the map object
#Use dict to convert the map object to a dictionary
result = dict(filter(None, map(filter_negative, ini_dict.items())))
 
#Print the resulting dictionary
print(result)
#This code is contributed by Edula Vinay Kumar Reddy

Output

{'a': 1, 'd': 7, 'e': 0}

The filter_negative() function takes a tuple representing a key-value pair in the dictionary, and returns the tuple if the value is non-negative. If the value is negative, the function returns None, which is filtered out by the filter() function. The map() function applies filter_negative() to each item in the dictionary, producing a list of tuples. The filter() function then filters out any None values, and the resulting list is passed to dict() to create the final filtered dictionary.

This approach has a time complexity of O(n), where n is the number of items in the dictionary. The space complexity is also O(n), as the filtered dictionary will have a size equal to the number of non-negative values in the original dictionary.

Method #5 : Using startswith() method

Python3




# Python code to demonstrate
# return the filtered dictionary
# on certain criteria
 
# 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
res = dict()
for i in list(ini_dict.keys()):
    x=str(ini_dict[i])
    if(not x.startswith("-")):
        res[i]=ini_dict[i]
     
print("resultant dictionary : ", str(res))

Output

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

Time Complexity : O(N)
Auxiliary Space : O(N)


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!