Categories
Python Answers

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

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *