3

Consider a very large Python dictionary of the like:

some_dict = {
    "val_1": "val_a",
    "val_2": "val_b",
    "val_x": "val_1",
    "val_y": "val_2",
    ### millions of one-to-one key-value pairs
    ### note that keys and values can overlap
}

I would like to use this dictionary to efficiently convert/translate data from one representation to another (and back). What is the most efficient way of achieving this?

One option is to create some_dict_reversed and then use dictionary looks ups for inverse conversion, but I would like to avoid duplicating the data. There must be a better way.

SultanOrazbayev
  • 14,900
  • 3
  • 16
  • 46
  • 1
    Because strings in Python are immutable, they can and will share the underlying data if you create a new dictionary from the same string objects. So there is no duplication. – Thomas Feb 18 '22 at 12:04
  • Thank you for this clarification, @Thomas! – SultanOrazbayev Feb 18 '22 at 12:11
  • 1
    @Thomas "no duplication" isn't true. The string objects will be shared, but all the references and hash values will be duplicated. – Pychopath Feb 19 '22 at 00:09

1 Answers1

2

If you want to use the values as keys, you need to hash them, which is what a dictionary does for you. So you'll need a second dictionary.

If the pairs indeed represent 1-to-1 relationships, i.e. it is a bijective relationship, then:

rev = { val: key for key, val in some_dict.items() }
trincot
  • 317,000
  • 35
  • 244
  • 286
  • Thank you, @trincot! I am experimenting with class definitions, and thought there might be some custom methods that allow something like an inverse of `__getitem__`. It seems reverse dictionary is the way to go. – SultanOrazbayev Feb 18 '22 at 12:15