Select Page

Bidirectional Hash table or Two way dictionary in Python

Suraj Gupta
Published: January 17, 2023

We know about Python dictionaries in a data structure in Python which holds data in the form of key: value pairs. In this article, we will discuss the Bidirectional Hash table or Two-way dictionary in Python. We can say a two-way dictionary can be represented as key ⇐⇒ value. One example of two-way dictionaries is:

Example:

dict={ 1 : 'Apple' , 2 : 'Google' , 3 : 'Microsoft'}

Input 1: 1
Output 1: Apple

Input 2: Microsoft
Output 2: 3

Explaination:The above dictionary maps the keys ( integers) to the values (company names) in this case.

A bidirectional dictionary can be represented as key ⇐⇒ value. I.e. it can return value based on the key and also the corresponding key on the basis of the value. In the above example, a regular dictionary can be looked up using 1,2,3 which would return Apple, Google, and Microsoft respectively. However in a bidirectional dictionary, we can look up the dictionary using 1,2, and 3 as well as Apple, Google, and Microsoft which would return 1,2,3 respectively.

Stepwise Implementation

Step 1: Installing the bidict library.

This library enables us to work with bidirectional hash tables or two-way dictionaries. To install the bidict library we need to use the following command:

pip install bidict

Step 2: Importing the bidict class from the bidict module

Python

from bidict import bidict

Step 3: Create a regular dictionary.

Creating a dictionary in python is simple. We will be creating a dictionary called dict_it_fullforms which maps the commonly used IT short forms to their full forms.

Python3

dict_it_fullforms={'APK':'Android Application Package',

                   'CPU':'Central Processing Unit',

                   'SMS':'Short Message Service',

                   'USB':'Universal Serial Bus',

                   'WIFI':'Wireless Fidelity',

                   'WWW':'World Wide Web'}

Step 4: Creating a bidict object

Create a 2-way dictionary by creating a bidict object bidict_it_fullforms using dict_it_fullforms.

Python3

bidict_it_fullforms = bidict(dict_it_fullforms)

Step 5: Lookup using short forms

Here we use the keys to print the values of bidict_it_fullforms.

Python3

print(bidict_it_fullforms['APK'])

print(bidict_it_fullforms['SMS'])

print(bidict_it_fullforms['WIFI'])

Output:

Android Application Package
Short Message Service
Wireless Fidelity

Step 6: An Inverse attribute of bidict object

In order to get the keys of respective full forms we need to use an inverse attribute of the bidict_it_fullforms object.

Python3

bidict_it_shortforms = bidict_it_fullforms.inverse

Step 7: Lookup using full forms

We now have bidict_it_shortforms as a bidict object reference that can be used to retrieve keys using values. Hence we can get the short forms using the full forms.

Python3

print(bidict_it_shortforms['Central Processing Unit'])

print(bidict_it_shortforms['Universal Serial Bus'])

print(bidict_it_shortforms['World Wide Web'])

Output:

CPU
USB
WWW

Step 8: Changes or Additions

If any changes or key-value additions are made to bidict_it_shortforms it will reflect in bidict_it_fullforms and vice versa. Let us add the full form of SIM. 

Python3

bidict_it_shortforms['Subscriber Identity Module']='SIM'

print(bidict_it_fullforms['SIM'])

Output:

Subscriber Identity Module

Complete Code:

Python3

  

from bidict import bidict

  

dict_it_fullforms = {'APK': 'Android Application Package'

                     'CPU': 'Central Processing Unit',

                     'SMS': 'Short Message Service'

                     'USB': 'Universal Serial Bus'

                     'WIFI': 'Wireless Fidelity'

                     'WWW': 'World Wide Web'}

  

bidict_it_fullforms = bidict(dict_it_fullforms)

  

print(bidict_it_fullforms['APK'])

print(bidict_it_fullforms['SMS'])

print(bidict_it_fullforms['WIFI'])

  

bidict_it_shortforms = bidict_it_fullforms.inverse

  

print(bidict_it_shortforms['Central Processing Unit'])

print(bidict_it_shortforms['Universal Serial Bus'])

print(bidict_it_shortforms['World Wide Web'])

  

bidict_it_shortforms['Subscriber Identity Module'] = 'SIM'

print(bidict_it_fullforms['SIM'])

Output:

Android Application Package
Short Message Service
Wireless Fidelity
CPU
USB
WWW
Subscriber Identity Module

Source: www.geeksforgeeks.org