Python Dictionary Search by value

In this Python tutorial, we will study How to search a value in Dictionary using some examples in python. Moreover, we will also cover these topics.

  • Python Dictionary Search by value
  • Python dictionary lookup by value
  • Python dictionary find value by key
  • Python find dict by value in list
  • Python dictionary find index by value
  • Python dictionary find max value
  • Python dictionary find minimum value
  • Python dict find and replace values
  • Python dictionary find unique values
  • Python list of dictionaries search by values
  • Python find specific value in dictionary
  • Python dictionary find duplicate values

Python dictionary search by value

  • In this section, we will discuss how to get the key by value in Python dictionary.
  • To perform this particular task, we are going to use the dict.items() method and this function return an object and the object is a collection of the key-value tuples.
  • To get the key element by the value, we are going to use the dict.get() method and this method returns the value for the specified key.

Syntax:

Let’s have a look at the Syntax and understand the working of dict.items() function in Python.

dict.items()

Note: This parameter does not contain any parameter and return’s a dictionary view.

Example:

Let’s take an example and check how to get the key by searching the value in the Python dictionary.

Source Code:

country_name = {'USA': 56, 'Germany': 25,'France':78}

result= dict((new_val,new_k) for new_k,new_val in country_name.items()).get(25)
print("Dictionary search by value:",result)

Here is the Screenshot of the following given code.

Python dictionary search by value
Python dictionary search by value

As you can see in the Screenshot the output displays the ‘Germany’ key as per the given value.

Read: Python dictionary key error

Python dictionary lookup by value

  • In this Program, we will discuss how to lookup the key by value in Python dictionary.
  • To do this task we are going to use the dictionary comprehension method and we can easily get the key element by searching the value.
  • In Python, dictionary comprehension is used to run a for loop on a dictionary and it will return a new dictionary.

Example:

Let’s take an example and check how to lookup the key by value in Python Dictionary.

Source Code:

my_dictionary = {'Newzealand': 567,
            'United Kingdom' : 456,
            'China': 945,
            'Japan': 845
            }
new_val = 945
result=[new_k for new_k in my_dictionary.items() if new_k[1] == new_val][0][0]
print(result)

In the following given code we have created a dictionary named ‘my_dictiobnary’ and then declare a variable ‘new_val’ and it indicates the value which we want to operate on it. After that we have used the dictionary comprehension method and set the condition if new_k[1] == new_val[0][0].

Here is the execution of the following given code.

Python dictionary lookup by value
Python dictionary lookup by value

Read: Python Dictionary Count + Examples

Python dictionary find value by key

  • In this Program, we will discuss how to find the value by key in Python dictionary.
  • By using the dict.get() function, we can easily get the value by given key from the dictionary. This method will check the condition if the key is not found then it will return none value and if it is given then it specified the value.
  • This method takes two arguments the key which you want to search and the default value to be returned.

Syntax:

Let’s have a look at the Syntax and understand the working of Python dict.get() function.

dict.get
        (
         key,
         default=None
        )

Example:

Let’s take an example and check how to find the value by key in the Python dictionary.

Source Code:

new_dict={'George':567,'James':145,'Micheal':678,'Oliva':234}

result= new_dict.get('Oliva')
print("Get value from key:",result)

Here is the execution of the following given code.

Python dictionary find value by key
Python dictionary find value by key

Read: Python Dictionary duplicate keys

Python find dict by value in list

  • In this section, we will discuss how to find the dict by value in the Python list.
  • To perform this particular task, first, we will create a list of dictionaries that contains elements in the form of key-value pairs. Next, we will declare a varaible ‘result’ and use the dictionary comprehension method.
  • In this example, we have to get the dictionary from the given list of dictionaries. To do this task we will set the condition if the specified key is equal to value then it will return a dictionary.

Example:

new_dictionary = [
     { "Student_name": "George", "Student_id": 156 },
     { "Student_name": "Oliva", "Student_id": 278 },
     { "Student_name": "James", "Student_id": 456 },
     { "Student_name": "William", "Studemt_id": 782 }
 ]

result=next(z for z in new_dictionary if z["Student_name"] == "James")
print(result)

Here is the Screenshot of the following given code.

Python find dict by value in list
Python find dict by value in a list

Read: Python dictionary increment value

Python dictionary find index by value

  • In this section, we will discuss how to find the index by value in a Python dictionary.
  • We have already covered this topic on Python dictionary index article. You get all the information regarding this topic.

Python dictionary find max value

  • In this Program, we will discuss how to find the max value in a Python dictionary.
  • To do this task, we are going to use the max() function to find the maximum value in a given dictionary
  • And to get the value from the dictionary, we will use the dictionary.get() method and it will return the value.

Example:

new_dictionary = {"USA": 167, "Germany": 278, "Japan": 678}

result = max(new_dictionary, key=new_dictionary.get)
print("Maximum value:",result)

In the above code, we have created a dictionary with different keys and values. Now we have to calculate the maximum value from the dictionary. To perform this task I have used the max() function along with dict.get() method and it will return the maximum value.

Here is the Screenshot of the following given code.

Python dictionary find max value
Python dictionary find max value

Read: Python dictionary extend

Python dictionary find minimum value

  • In this Program, we will discuss how to find the minimum value in a Python dictionary.
  • To do this task we are going to use the min() function and this function will help the user to get the minimum value from dictionary.

To get more information regarding how to find the minimum value in a dictionary. You can refer to our detailed article Python find max value in dictionary.

Example:

country_name = {"United kingdom": 567, "France": 167, "Spain": 554}

result = min(country_name, key=country_name.get)
print("Minimum value:",result)

Here is the implementation of the following given code

Python dictionary find minimum value
Python dictionary find the minimum value

Python dict find and replace values

  • In this Program, we will discuss how to replace values in Python dictionary.
  • To do this task first we have created a dictionary named ‘new_dictionary’ that contains elements in the form of key-value pair and then we will set and update the key value.
  • Once you will execute this code the output displays the new value for the specified key.

Example:

Let’s take an example and check how to replace values in a Python dictionary.

Source Code:

new_dictionary  =  {
   'George': 156,
   'James': 278,
   'William':134
}
new_dictionary['George'] = 189  
print(new_dictionary)

You can refer to the below Screenshot.

Python dict find and replace values
Python dict find and replace values

Read: Python dictionary multiple keys

Python dictionary find unique values

  • In this section, we will discuss how to find the unique values in the Python dictionary.
  • To get the unique values from the dictionary we are going to use the set() method along with dict.values() method. In Python, the set() method must be unique and it removes all duplicate elements from objects we can also use to perform mathematical operations.
  • In Python, the dict.values() method will help the user to extract values from dictionary.

Example:

Let’s take an example and check how to find the unique values in the Python dictionary.

Source Code:

new_dictionary = {'USA' : 586,
   'United Kingdom' : 9872,
   'France' : 586,
   'Germany' : 234,
   'Newzealand' :189}
   
print("Original dictionary:",new_dictionary)
result=set(new_dictionary.values())
print("Unique values from dictionary:",result)

In the following given code, we have created a dictionary ‘new_dictionary’.After that, we have used the set() function and it will get the unique values from the dictionary.

Here is the execution of the following given code.

Python dictionary find unique values
Python dictionary find unique values

Read: Python Dictionary to CSV

Python list of dictionaries search by values

  • In this Program, we will discuss how to find the value in list of dictionaries in Python.
  • In this particular task, we are going to use the dict.get() method and within this method, we have set the condition if the given value is available in the list of dictionaries then it will return the dictionary.

Syntax:

Let’s have a look at the Syntax and understand the working of dict.get() method.

dict.get(key[, value])

Example:

Let’s take an example and check how to find the value in list of dictionaries.

Source Code:

new_dictionary = [{'George':145,'Oliva':245},
                {'William':567,'John':234},
                {'Micheal':234,'james':567}]

print("Original dictionary:",new_dictionary)
new_result = next((m for m in new_dictionary if m.get('John') == 234), None)
print("Get dictionary from searching value:",new_result)

In the above example, we have created a dictionary and then we use the dictionary comprehension method in which we have set the condition if m.get(‘john’)==234.

Here is the Screenshot of the following given code.

Python list of dictionaries search by values
Python list of dictionaries search by values

Read: Python convert dictionary to an array

Python find specific value in dictionary

  • Here we are going to check how to find the specific value in Python dictionary.
  • To perform this particular task, we are going to use the dict.get() method and assign the key element. Once you will execute this code the output displays the value of the given specified key.

Example:

new_dictionary={'Student_name': 'George', 'Student_id': 156,'Student_age':745}

result= new_dictionary.get('Student_id')
print(result)

Here is the Output of the following given code.

Python find specific value in dictionary
Python find specific value in the dictionary

Read: Python dictionary of tuples

Python dictionary find duplicate values

  • Here we are going to discuss how to find the dupliacte values in Python dictionary.
  • First, we will create a dictionary, and then we are going to use the dict.items() method.
  • Next, we will set the condition if value contains in the empty dictionary then it will equate with a key element, and else it will append with the key value.

Example:

Let’s take an example and check how to find the duplicate values in Python Dictionary.

Source Code:

new_dictionary = {'Grapes':245, 'Apple':567, 'Mangoes': 156, 'Oranges': 567}

empty_dict = {}
for new_k, new_val in new_dictionary.items():
   if new_val not in empty_dict:
      empty_dict[new_val] = [new_k]
   else:
      empty_dict[new_val].append(new_k)
print("Duplicate values in dictionary:",empty_dict)

Here is the implementation of the following given code

Python dictionary find duplicate values
Python dictionary find duplicate values

You may also like to read the following tutorials on Python.

In this Python tutorial, we have studied Python Dictionary Search by value using some examples in python. Moreover, we have also covered these topics.

  • Python Dictionary Search by value
  • Python dictionary lookup by value
  • Python dictionary find value by key
  • Python find dict by value in list
  • Python dictionary find index by value
  • Python dictionary find max value
  • Python dictionary find minimum value
  • Python dict find and replace values
  • Python dictionary find unique values
  • Python list of dictionaries search by values
  • Python find specific value in dictionary
  • Python dictionary find duplicate values