We learn about Python dictionaries in a knowledge construction in Python which holds knowledge within the type of key: worth pairs. On this article, we are going to talk about the Bidirectional Hash desk or Two-way dictionary in Python. We are able to 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 well return worth primarily based on the important thing and in addition the corresponding key on the idea of the worth. Within the above instance, a daily dictionary could be appeared up utilizing 1,2,3 which might return Apple, Google, and Microsoft respectively. Nonetheless in a bidirectional dictionary, we are able to search for 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 straightforward. We might be making a dictionary referred to as dict_it_fullforms which maps the generally used IT brief 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 brief types
Right here we use the keys to print the values of bidict_it_fullforms.
Python3
|
Output:
Android Utility Package deal Quick Message Service Wi-fi Constancy
Step 6: An Inverse attribute of bidict object
With a purpose 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 brief types utilizing the complete types.
Python3
|
Output:
CPU USB WWW
Step 8: Adjustments or Additions
If any adjustments or key-value additions are made to bidict_it_shortforms it should replicate in bidict_it_fullforms and vice versa. Allow us to add the complete type of SIM.
Python3
|
Output:
Subscriber Id Module
Full Code:
Python3
|
Output:
Android Utility Package deal Quick Message Service Wi-fi Constancy CPU USB WWW Subscriber Id Module