-1

I've created a dictionary like

d = {1: {"a":1, "b":2}, 2: {"a":1, "b":2}}

I'm looping through objects to create dictionaries like the above.

I also want to create a reverse of the above dictionary while I loop, it should look like

d2 = {{"a":1, "b":2}: 1, {"a":1, "b":2}: 2}

I know that the dictionary is an unhashable type, but at the same time I want the ability to reverse look up values without looping through the dictionary.

Is there some way to do this in python?

Derek
  • 11,980
  • 26
  • 103
  • 162

1 Answers1

3

I also want to create a reverse of the above dictionary while I loop, it should look like

  1. No, you can't. All keys to a dictionary should be hash-able and a dictionary is not hash-able
  2. Key's cannot have duplicate entries

Is there some way to do this in python?

Unless you wan't the key's to be dictionary, you can convert to some other data structure. May be frozenset of item lists?

If you need duplicate keys, use frozenset of item lists with MultiMap 1.0.3

And google returned me an implementation of frozen dict, you can use it with MultiMap 1.0.3

Abhijit
  • 62,056
  • 18
  • 131
  • 204