Find key from value in dictionary python
Python:
Python programming language is one of the most used programming languages, as it is used widely in software and data analysis, web development, etc. It is said to be a user-friendly programing language, as its syntax is very simple to write and easy to understand for a beginner programmer. It is used in many popular companies worldwide like google, uber, Instagram, amazon, etc. Python programming language is considered a general purpose; it is a high-level programming language that is not much difficult but easier to learn. Python programming language is rich in libraries that can be imported easily and used to perform many different operations. In the year 1989, Guido van Rossum is the one who introduced python programming language. Python is widely used in machine learning, and data science departments deal with heavy data. It is also used in web applications; web applications like the Django and Flask frameworks are created using python. The python programming language is straightforward to learn by anyone. Compared to any programming language, the syntax in python is much easier. Many colleges and institutions have introduced python in their syllabus so that the students need to learn python. The biggest advantage of the python programming language is that it has a good collection of libraries that are widely used in machine learning, web frameworks, test frameworks, multimedia, image processing, and many more applications.
Dictionaries in python
Dictionaries in python in python are considered to be a collection with key values. It is used to store the data values of a map compared to other data types, which have only one value as the element but more than one value as an element. Now let us look into an example of how a dictionary is in python program programming language. Dictionary has a key: value format. The key value is provided in the python dictionary to make it more optimized. Now let's look into an example code in a python programming language to create a dictionary and print it.
Example code:
# # # creating a dictionary in python programming language
D = { 1 : 'ironman' , 2 : 'captainamerica' , 3: 'thor' , 4 : 'blackpanther' , 5 : 'vision'}
# # # printing the created dictionary
print( D )
Output
{ 1 : ‘ironman’ , 2 : ‘captainamerica’ , 3: ‘thor’ , 4 : ‘blackpanther’ , 5 : ‘vision’ }
Creating a dictionary in a python programming language is similar to having elements in the curly braces ( { } ) which are separated by a comma (, ). Generally, a dictionary has two values. One is considered a key, and the other is its corresponding key: value. The values in the dictionary can be of any data type and can be repeated as often as required, but the keys are not supposed to be repeated, and these are immutable, which means once they are declared, they cannot be changed. The keys of the dictionary are case-sensitive. Now let's understand in a better way how to create a dictionary with integer keys and different datatypes as keys.
Example code
# # # creating a a dictionary in pyhton programming language with integer as keys
D = { 1 : 'ironman' , 2 : 'captainamerica' , 3: 'thor' , 4 : 'blackpanther' , 5 : 'vision'}
# # # printing the created dictionary
print("\nusing integer as keys the dictionary is : ")
print( D )
D = { 'name' : 'ravan' , 1 : [ 2, 3, 4, 5, 6 , 7, 8, 9, 0] }
print("\n using mixed keys the dictionary is: ")
print( D )
Output:
using integer as keys the dictionary is :
{ 1 : ‘ironman’ , 2 : ‘captainamerica’ , 3: ‘thor’ , 4 : ‘blackpanther’ , 5 : ‘vision’ }
using mixed keys the dictionsry is:
{ ‘name’ : ‘ravan’ , 1 : [ 2, 3, 4, 5, 6 , 7, 8, 9, 0 ]
We can also create a dictionary by using a built function called dict( ). one can create an empty dictionary by simply using empty curly braces { }. now let's look-see the example code of how to create an empty dictionary with the use of a built I function called dict( ).
Example code:
# # # creating an empty dictionary
D = { }
# # # printng the empty dictionary
print('\nthe empty dictionary is: ' )
print( D )
# # # creating a dictonary with the help of built in functoin called dict( )
D = { 1 : 'ironman' , 2 : 'captainamerica' , 3: 'thor' , 4 : 'blackpanther' , 5 : 'vision'}
print( '\ndictionary with the help of dict( ): ' )
print( D )
Output
the empty dictionary is:
{ }
dictoinary with the help of dict( ):
{ 1 : ‘ironman’ , 2 : ‘captainamerica’ , 3: ‘thor’ , 4 : ‘blackpanther’ , 5 : ‘vision’ }
Now we can create a dictionary; let's see how to find the key from the value in the dictionary in a python programming language.
Finding key from value in dictionary
Let's find or extract the key from the dictionary using the value. First, let's discuss the method to get the key value using the list comprehension method. This method consists of brackets that have the expression; it is executed for each element in the dictionary along the for loop, which is iterated over each element in the python list to find the key from the value in the python dictionary.
Example code:
D = { 1 : 'ironman' , 2 : 'captainamerica' , 3: 'thor' , 4 : 'blackpanther' , 5 : 'vision'}
V = { I for I in D if D[ I ] == 'captainamerica' }
print( "key by value:" , V )
Output:
Key by value: { 2 }
Now lets Discuss the method using the list.index( ). This method returns the index as the value in a list.in this method, it is found two different lists of keys and values. Then using, the position of the value from the V_list key is fetched. The key at any position N in the key_list has the corresponding value at the position N in the V_list. Now lets us look into an example code.
Example code :
# # # creating a dictionary
D = { 'rohith' : 100 , 'vardhan' : 200 , 'taimur' : 300 , 'kiran' : 400 , 'ravi' : 500 , 'ram' : 600 }
# # # printing the crreated dictionary
print( D )
Key_list = list( D.keys( ) )
V_list = list( D.values( ) )
# # # printing the key having the value of 300
pos = V_list.index( 300 )
print( Key_list[ pos ] )
Output
{ “rohith” : 100 , “vardhan” : 200 , “taimur” : 300 , “kiran” : 400 , “ravi” : 500 , “ram” : 600 }
taimur
Now let's look into another method, finding the key for value using dict.item( )
In this method, we fetch the key from a value by matching the values using the dict.item( ) and then get the corresponding key value for the given value. Now let us look at the example program to understand it better.
Example code:
# # # creating a dictionary
D = { 'rohith' : 100 , 'vardhan' : 200 , 'taimur' : 300 , 'kiran' : 400 , 'ravi' : 500 , 'ram' : 600 }
print( D )
# # # creating a function to return key of any value
def get_key( val ) :
for key, value in D.items( ) :
if val == value :
return key
return "the key doesn’t exist "
print( get_key( 200 ) )
print( get_key( 300 ) )
Output
{ “rohith” : 100 , “vardhan” : 200 , “taimur” : 300 , “kiran” : 400 , “ravi” : 500 , “ram” : 600 }
vardhan
taimur