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

  • SEARCH
  • Home
  • 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 9148719
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T11:21:02+00:00 2026-06-17T11:21:02+00:00

Possible Duplicate: Efficient bidirectional hash table in Python? I’m working on an AST parser,

  • 0

Possible Duplicate:
Efficient bidirectional hash table in Python?

I’m working on an AST parser, that parses python code. First, I turn the python code into an AST with a call to compile.
In doing this, I need to ensure that I don’t compile the same imported module twice, even if there are two calls to import it.

Currently, I can detect that these two calls are equivalent:

import modname as mod
import modname as mod

I maintain a dictionary that maps (in this case) mod to modname. I use this not only to detect that modname has already been imported, but also for some other bookkeeping functions down the road.

Now, I can’t detect that the following three calls import the same module:

import modname as mod
import modname as foo
import modname

I know that I can easily circumvent this problem by using a set to contain modname and check this set before I compile modname the second time. However, this requires another block of linear space.
I could do a linear pass over the dictionary to check if any key maps to a value of modname, but that defeats the purpose of using a dict.

Hence my question: does there exist a data structure that’s a “two-way dict” – one that maps it’s keys to values and whose values can be looked up at O(1) time as well?

  • 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-17T11:21:04+00:00Added an answer on June 17, 2026 at 11:21 am

    Python already tracks imported modules, in sys.modules.

    Each key is a imported module name (and not the alias under which it was imported), each value is the actual module object with the global namespace for that module:

    >>> import sys
    >>> import os
    >>> 'sys' in sys.modules
    True
    >>> 'os' in sys.modules
    True
    >>> sys.modules['sys'].__dict__.keys()
    ['setrecursionlimit', 'dont_write_bytecode', 'getrefcount', 'path_importer_cache', 'stdout', 'getprofile', '__stdin__', 'version_info', 'exc_clear', 'prefix', 'getfilesystemencoding', 'byteorder', '_clear_type_cache', 'excepthook', 'ps1', 'exc_type', '__excepthook__', 'executable', 'float_info', 'copyright', 'setdlopenflags', 'exec_prefix', 'getdlopenflags', 'getrecursionlimit', 'py3kwarning', 'path_hooks', '__package__', '_current_frames', 'platform', 'maxsize', 'version', 'exit', 'call_tracing', 'callstats', 'flags', 'setcheckinterval', '__doc__', 'api_version', '__plen', 'getdefaultencoding', 'getcheckinterval', 'maxunicode', 'settrace', 'setprofile', 'argv', '__stdout__', 'meta_path', '__name__', 'subversion', 'builtin_module_names', 'stdin', '__stderr__', '__egginsert', 'displayhook', 'ps2', 'gettrace', 'modules', 'warnoptions', 'last_type', 'getsizeof', 'last_traceback', 'maxint', '__displayhook__', '_getframe', 'stderr', 'exc_info', 'path', 'last_value', 'hexversion']
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Efficient way of parsing fixed width files in Python Not even sure
Possible Duplicate: Learning efficient algorithms I recently came across an problem that was solved
Possible Duplicate: Python’s most efficient way to choose longest string in list? I have
Possible Duplicate: Python: Elegant and efficient ways to mask a list I have two
Possible Duplicate: efficient word indexing in python Apologies for the basic question, how can
Possible Duplicate: Power Efficient Software Coding Adobe announced at Google I/O that it's next
Possible Duplicate: Need help in building efficient exhaustive search algorithm Imagine that you must
Possible Duplicate: What is the most efficient/elegant way to parse a flat table into
Possible Duplicate: && operator in Javascript In the sample code of the ExtJS web
Possible Duplicate: What is the most efficient way to clone a JavaScript object? How

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.