We learn about Python dictionaries in an information construction in Python which holds knowledge within the type of key: worth pairs. On this article, we’ll talk about the Bidirectional Hash desk or Two-way dictionary in Python. We will say a two-way dictionary could be represented as key ⇐⇒ worth. One instance of two-way dictionaries is:
Instance:
dict={ 1 : 'Apple' , 2 : 'Google' , 3 : 'Microsoft'}
Enter 1: 1
Output 1: Apple
Enter 2: Microsoft
Output 2: 3
Explaination:The above dictionary maps the keys ( integers) to the values (firm names) on this case.
A bidirectional dictionary could be represented as key ⇐⇒ worth. I.e. it may possibly return worth based mostly on the important thing and likewise the corresponding key on the premise of the worth. Within the above instance, a daily dictionary could be regarded up utilizing 1,2,3 which might return Apple, Google, and Microsoft respectively. Nonetheless in a bidirectional dictionary, we are able to lookup the dictionary utilizing 1,2, and three in addition to Apple, Google, and Microsoft which might return 1,2,3 respectively.
Stepwise Implementation
Step 1: Putting in the bidict library.
This library permits us to work with bidirectional hash tables or two-way dictionaries. To put in the bidict library we have to use the next command:
pip set up bidict
Step 2: Importing the bidict class from the bidict module
Python
|
|
Step 3: Create a daily dictionary.
Making a dictionary in python is easy. We will probably be making a dictionary known as dict_it_fullforms which maps the generally used IT quick types to their full types.
Python3
|
|
Step 4: Making a bidict object
Create a 2-way dictionary by making a bidict object bidict_it_fullforms utilizing dict_it_fullforms.
Python3
|
|
Step 5: Lookup utilizing quick types
Right here we use the keys to print the values of bidict_it_fullforms.
Python3
|
|
Output:
Android Software Bundle Quick Message Service Wi-fi Constancy
Step 6: An Inverse attribute of bidict object
To be able to get the keys of respective full types we have to use an inverse attribute of the bidict_it_fullforms object.
Python3
|
|
Step 7: Lookup utilizing full types
We now have bidict_it_shortforms as a bidict object reference that can be utilized to retrieve keys utilizing values. Therefore we are able to get the quick types utilizing the total types.
Python3
|
|
Output:
CPU USB WWW
Step 8: Modifications or Additions
If any adjustments or key-value additions are made to bidict_it_shortforms it can replicate in bidict_it_fullforms and vice versa. Allow us to add the total type of SIM.
Python3
|
|
Output:
Subscriber Identification Module
Full Code:
Python3
|
|
Output:
Android Software Bundle Quick Message Service Wi-fi Constancy CPU USB WWW Subscriber Identification Module