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 8683701
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T22:06:59+00:00 2026-06-12T22:06:59+00:00

I have 20 strings which match 20 integers. All integers and strings are unique

  • 0

I have 20 strings which match 20 integers. All integers and strings are unique. I am considering but would like to avoid creating two dictionaries. One dictionary will be indexed by the string, and one dictionary will be indexed by the integer.

  • How should I handle this situation?

I created two lists. One containing strings, the other containing integers. I am thinking of building two functions. One function will produce a string. The other function will produce an integer. Another alternative is to combine these into one function by branching if produced argument is an integer or a string.

  • How is this comparable to a dictionary? Will it consume a lot of cpu?
    (this function will run millions of times everyday)
  • Should I just create a list of tuples which are (string, int) and then
    create two dictionaries, one mapping int to list position and other
    string to list position? Will it be the best way?

I don’t have many items so I can sacrifice some memory.

Please explain the best method with an explanation of why it is the best.

Thank you.

  • 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-12T22:07:00+00:00Added an answer on June 12, 2026 at 10:07 pm

    Whatever solution you go with, if you want it to be robust, you should probably wrap a class around it that automatically updates the other direction when you update one. For instance, here’s a start at a basic bidirectional dictionary using @mgilson’s technique (which means it won’t work if there’s any overlap between the two sets of items you’re mapping to each other; having different types works great, though):

    class BiDict(dict):
      """Bidirectional Dictionary - setting 'key' to 'value' also
         sets 'value' to 'key' (so don't use overlapping mappings)
      """
    
      def __init__(self, *args):
        super(BiDict, self).__init__(*args)
    
        # After regular dict initialization, loop over any items
        # and add their reverse.  Note that we can't use any of the
        # iter* methods here since we're adding items in the body
        # of the loop.
        for key in self.keys():
          super(BiDict, self).__setitem__(self[key], key);
    
    
      def __setitem__(self, key, val):
        # If the key has an old value, delete its reverse
        if key in self:
          super(BiDict, self).__delitem__(self[key])
    
        # Then add both forward and reverse for the new value
        super(BiDict, self).__setitem__(key, val);
        super(BiDict, self).__setitem__(val, key);
    
      def __delitem__(self, key):
        # delete both directions
        if key in self:
          super(BiDict, self).__delitem__(self[key]);
          super(BiDict, self).__delitem__(key);
    

    You can use it like this:

    >>> from bidict import BiDict
    >>> d = BiDict({'a':1,'b':2})
    >>> d['a']
    1
    >>> d[2]
    'b'
    >>> d['c']=3
    >>> d[3]
    'c'
    >>> del d['a']
    >>> d['a']
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    KeyError: 'a'
    >>> d[1]
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    KeyError: 1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the strings which consists of ( ' ) quote mark like mother's
I have to work with strings which may contain Lat/Long data, like this: $query
Is this Regex correct if I have to match a string which is atleast
I faced an interesting problem today. I have 4 strings which I need to
I have a vector of strings which are changing its contents for no apparent
I have a number of strings which contain words which are bunched together and
I have strings that contain a variable number of leading hyphens and which may
Example of the problem If I have a list of valid option strings which
Possible Duplicate: A better way to compare Strings which could be null I have
In Oracle, I have a table with a column (X) which can have strings

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.