Bidirectional Hash table or Two way dictionary in Python

Hey Geek! In this tutorial, we will implement a two-way dictionary or a bidirectional hashmap in Python. Before getting started you need to be familiar with the dictionary data structure in Python. In case you are not familiar with the dictionary do check this out.
Dictionary in Python

Let us now get familiar with a bidirectional hashmap in python.

Bidirectional hashmap in Python

We know that Dictionary Data Structure in Python stores the data in the form of key and value pairs. Now it is easier to understand the concept of a bi-directional dictionary.

A bidirectional dictionary is a dictionary in Python that can return the respective value based on the key and also the respective key based on the value.

Example:
Let symbol_state be a bidirectional dictionary {‘WB’:’West Bengal’,’KL’:’Kerala’}
then symbol_state[‘WB’] will return ‘West Bengal’,
symbol_state[‘KL’] will return ‘Kerala’ and also
symbol_state.inverse[‘Kerala’] will return ‘KL’.

There are a few ways in which we can work with bidirectional hashmaps. In this tutorial, we will use a predefined bi-directional dictionary.

So, let us first start with installing the bidirectional mapping library for python using the below command.

pip install bidict

The bidict library provides an implementation of bidict bi-directional hashmap/dictionary data structure in Python.

Let us get into a program to understand it much better.

Start with importing the bidict class from bidict library.

from bidict import bidict

Now, let us create a dictionary dic_symbol_state with some states of our countries as keys and their symbols as values as discussed in the previous example.

dic_symbol_state = {'WB':'West Bengal', 'KL':'Kerala', 'TS':'Telangana','AP':'Andhra Pradesh'}

We will now be creating a bidict object bidict_symbol_state  using the dic_symbol_state. It can be done as shown below.

bidict_symbol_state = bidict(dic_symbol_state)

Let us try printing state names(values) using state symbols(keys) through bidict_symbol_state.

print(bidict_symbol_state['KL'])
print(bidict_symbol_state['WB'])
print(bidict_symbol_state['TS'])

To get state symbols(keys) of respective state names(values) we need to use an inverse attribute of the bidict_symbol_state object.

bidict_state_symbol = bidict_symbol_state.inverse

Now, bidict_state_symbol is a bidict object reference that can be used to get keys using values. So, we can now get state symbols using respective state names through bidict_state_symbol.

print(bidict_state_symbol['Telangana'])

Here any changes or updations made to bidict_state_symbol will reflect in bidict_symbol_state and vice versa.

bidict_state_symbol['Andhra Pradesh']='AP'
print(bidict_symbol_state['AP'])

Program

from bidict import bidict

dic_symbol_state = {'WB':'West Bengal', 'KL':'Kerala', 'TS':'Telangana'}
bidict_symbol_state = bidict(dic_symbol_state)

print(bidict_symbol_state['KL'])
print(bidict_symbol_state['WB'])
print(bidict_symbol_state['TS'])

bidict_state_symbol = bidict_symbol_state.inverse

print(bidict_state_symbol['Telangana'])

bidict_state_symbol['Andhra Pradesh']='AP'
print(bidict_symbol_state['AP'])

Output

Kerala
West Bengal 
Telangana 
TS
Andhra Pradesh

Yahoo! We have successfully implemented a bi-directional hashmap in Python.
Thank you for reading the tutorial. I hope you have found this useful. In case of any doubts, feel free to post them below. Also do check out our other related articles below:

Leave a Reply

Your email address will not be published.