Python Program to Search an Element in a Dictionary


Dictionaries are used to store data values in key:value pairs like maps (which unlike other data types holds only a single value as an element). key:value is provided in dictionaries to make it more effective. Keys are unique. Dictionary key must be unique. So, no duplicate values are allowed in dictionaries. Dictionaries items are ordered, changeable, immutable. Changeable means here is that, we can add or remove items after the dictionaries are created.

In this article, we will know about how to search an element in dictionary by using different functions. There are many functions to search an element in dictionary by using different method like “for” and “in”, list.index(), dict.item(), by using item+list etc.

How to Search an Element From a Dictionary?

Here, we have given a dictionary of numbers and we have to search an element from a dictionary by using different methods. There four methods to search an element in a dictionary.

  • By using “for” and “in” loop

  • By Using items() + list

  • By using dict.item()

  • By using list.index()

Using “for” and “in” Loop

A for loop is used to execute a statement repeatedly until given condition is satisfied. And when condition becomes false, the line immediately after the loop in the program is executed.

The “in” operator determines whether a given value is constituent element of a sequence such as a string, array, list, or tuple, dictionary. It is used to search an element in a dictionary. For example −

Example

Here, we have a program in which we have used “for loop” for searching an element in a dictionary. In this program we have searched “value=3” element that exists in a dictionary. So, output comes as “cherry”.

fruit = {'apple' : 1, 'mango' : 2, 'cherry' : 3}
print("The original dictionary is : " + str(fruit))
val = 3
for key in fruit:
   if fruit[key] == val:
      res = key
print("The key corresponding to value : " + str(res))

Output

The original dictionary is : {'apple': 1, 'mango': 2, 'cherry': 3}
The key corresponding to value : cherry

Using items + list

List is a data structure in python, which is changeable, mutable and iterable ordered sequence of element. They are used to store multiple elements in a single variable. List allows duplicate elements.

By using the items(), which is used to extract both keys and values at once, hence making the search easy and can be executed using list makes process of searching a elements in a dictionary easy.

Example

Here, we have a program in which we have used “item()+list” for searching an element in a list. In this program, we have searched “value=2” element that exists in a dictionary. So, output comes as ”mango”.

fruit = {'apple' : 1, 'mango' : 2, 'cheery' : 3}
print("The original dictionary is : " + str(fruit))
val = 2
res = [key for key, value in fruit.items() if value == val]
print("The key corresponding to value : " + str(res))

Output

The original dictionary is : {'apple': 1, 'mango': 2, 'cheery': 3}
The key corresponding to value : ['mango']

Using dict.item()

By using the dict.items(), which is used to extract both keys and values from a value by matching all the values.

Example

Here, we have a program in which we have used “dict.item()” to searching an element in a list. In this program we have searched “100 and 11” element that exists in a dictionary. So, output comes as ”java and C”.

def get_key(val):
   for key, value in program.items():
      if val == value:
         return key
   return "key doesn't exist"
program = {"Java": 100, "Python": 112, "C": 11}
print(get_key(100))
print(get_key(11))

Output

Java
C

Using list.index()

This is one liner code for searching an element in a dictionary by using indentation.

Example

Here, we have a program in which we have used “list.index()” for searching an element in a dictionary. In this program, we have search index “112” element that exists in a dictionary. So, output comes as ”python”.

program = {"Java":100, "Python":112, "C":11}
print("One line Code Key value: ", list(program.keys())
   [list(program.values()).index(112)])

Output

One line Code Key value: Python

Conclusion

In this article, we have briefly discussed about the different methods used to search an element in a dictionary.

Updated on: 24-Apr-2023

9K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements