How to create a two way/reverse map with Python?

Sometimes, we want to create a two way/reverse map with Python.

In this article, we’ll look at how to create a two way/reverse map with Python.

How to create a two way/reverse map with Python?

To create a two way/reverse map with Python, we put the forward and reverse maps in the same dictionary.

For instance, we write

relation = {}
relation['Alice'] = 'Bob'
relation['Bob'] = 'Alice'

to create the relation dict.

Then we add both the mapping from 'Alice' to 'Bob' and vice versa in the same dictionary.

Conclusion

To create a two way/reverse map with Python, we put the forward and reverse maps in the same dictionary.