I mean a dictionary where of you could get the value by key or the key by value depending on what you need.
Asked
Active
Viewed 1,151 times
1
-
2Use two dicts . – interjay Mar 18 '17 at 11:04
-
What is your final goal / use case? Think of issues like duplicated keys/values. There are already similar answers on SO: http://stackoverflow.com/questions/483666/python-reverse-invert-a-mapping – pansen Mar 18 '17 at 11:21
1 Answers
4
You could use bidict package which provides a bidirectional map. The syntax looks as follows (taken from the documentation):
>>> from bidict import bidict
>>> element_by_symbol = bidict(H='hydrogen')
>>> element_by_symbol
bidict({'H': 'hydrogen'})
>>> element_by_symbol['H']
'hydrogen'
>>> element_by_symbol.inv
bidict({'hydrogen': 'H'})
>>> element_by_symbol.inv['hydrogen']
'H'
>>> element_by_symbol.inv.inv is element_by_symbol
True
Or you can implement it yourself, for example using one of the solutions provided here.

Community
- 1
- 1

Miriam Farber
- 18,986
- 14
- 61
- 76