Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8111523
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T02:09:24+00:00 2026-06-06T02:09:24+00:00

I am looking for a data structure that holds the same values under two

  • 0

I am looking for a data structure that holds the same values under two different indexes, where I can access the data by either one.

Example:

x = mysticalDataStructure()
x.add(1,'karl', dog)
x.add(2,'lisa', cat)

$ x[1].age
2
$ x['karl'].age
2
$ x[1].age = 4
$ x['karl'].age
4

Is there anything prerolled, or what is the best approach to roll my own (I need access via an index (number going from 0 to n in increments of 1), and via a string).

collections.ordereddict does not seem to have fast random access via the position, as far as I see I can only walk it with the iterator until I reach element i (I can insert in the right order).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T02:09:25+00:00Added an answer on June 6, 2026 at 2:09 am
    class MultiKeyDict(object):
    
        def __init__(self, **kwargs):
            self._keys = {}
            self._data = {}
            for k, v in kwargs.iteritems():
                self[k] = v
    
        def __getitem__(self, key):
            try:
                return self._data[key]
            except KeyError:
                return self._data[self._keys[key]]
    
        def __setitem__(self, key, val):
            try:
                self._data[self._keys[key]] = val
            except KeyError:
                if isinstance(key, tuple):
                   if not key:
                      raise ValueError(u'Empty tuple cannot be used as a key')
                   key, other_keys = key[0], key[1:]
                else:
                   other_keys = []
                self._data[key] = val
                for k in other_keys:
                    self._keys[k] = key
    
        def add_keys(self, to_key, new_keys):
            if to_key not in self._data:
                to_key = self._keys[to_key]
            for key in new_keys:
                self._keys[key] = to_key
    
    
        @classmethod
        def from_dict(cls, dic):
            result = cls()
            for key, val in dic.items():
                result[key] = val
            return result
    

    Usage:

    >>> d = MultiKeyDict(a=1, b=2)
    >>> d['c', 'd'] = 3 # two keys for one value
    >>> print d['c'], d['d']
    3 3
    >>> d['c'] = 4
    >>> print d['d']
    4
    >>> d.add_keys('d', ('e',))
    >>> d['e']
    4
    >>> d2 = MultiKeyDict.from_dict({ ('a', 'b'): 1 })
    >>> d2['a'] = 2
    >>> d2['b']
    2
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am looking for a structure that holds a sorted set of double values.
I'm looking for a data structure that will store any DAG, but can efficiently
I'm looking for a data structure that provides indexing for Rectangles. I need the
I'm looking for a Java built-in data structure that would be the best at
I'm looking for a data structure (or structures) that would allow me keep me
I'm looking for a sorted data structure, that will be similar to the STL
I'm looking for a tool that finds duplicate nodes in a tree data structure
I am looking for a data structure that will sort objects based on a
As ArrayCollection in Flex is limited, I'm looking for a data-structure that something like
I am looking for a data structure in Python that is similar to a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.