0

I was wondering if Python offers a dictionary-like data structure that allows bidirectional lookup by both key and value and requires that both be unique in their realm. The regular dictionary requires only the key to be unique and the search of keys by a value is not indexed in the same way the other way around is, meaning a full scan is required.

My concrete case scenario is something like a structure of items that have a name and a nickname and, within a structure, both have to be unique (while it is allowed that one item's name be the same as another or the same item's nickname). I need to be able to easily look up a name by a nickname and vice versa and I was wondering if there is a way to do it within a single structure. Of course, I can accomplish it by having two separate structure, one mapping nicknames to names and the other mapping names to nicknames.

There are similar posts but they are old and I was wondering if there have been any developments in the meantime.

Community
  • 1
  • 1
amphibient
  • 29,770
  • 54
  • 146
  • 240
  • If you'd like more attention and the chance of new answers, consider a bounty. – jonrsharpe Dec 08 '16 at 17:14
  • I'm not sure if a dict is the best structure for that case... I think it's better if you create your own class with a data structure on it. In this new class you can easily check the uniqueness of that data structure with a lot of flexibility. – javidgon Dec 08 '16 at 17:15
  • I noted that this question is similar to the one you linked as duplicate but I also said the other one is old and was looking to shed some light on possible developments since 6 years ago – amphibient Dec 08 '16 at 17:16
  • I understand that; see the comment above. It's not really clear what "developments" you're expecting either. If you're asking if it's now in the standard library, then no it isn't (see `collections` where I'd guess this would live if it existed). If you're looking for third-party implementations, that's not on-topic either. – jonrsharpe Dec 08 '16 at 17:21

0 Answers0