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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T02:18:42+00:00 2026-05-19T02:18:42+00:00

I need to use a big data structure, more specifically, a big dictionary to

  • 0

I need to use a big data structure, more specifically, a big dictionary to do the looking up job.

At the very first my code is like this:

#build the dictionary
blablabla
#look up some information in the ditionary
blablabla

As I need to look up many times, I begin to realize that it is a good idea to implement it as a function, say lookup(info).

Then here comes the problem, how should I deal with the big dictionary?

Should I use lookup(info, dictionary) to pass it as an argument, or should I just initialize the dictionary in main() and just use it as an global variable?

The first one seems more elegant because I think maintaining global variable is troublesome.
But on the other hand, I’m not sure of the efficiency of passing a big dictionary to a function. It will be called many times and it will certainly be a nightmare if the argument passing is inefficient.

Thanks.

Edit1:

I just made an experiment of the above two ways:

Here’s the snippet of the codes. lookup1 implements the argument passing looking up while lookup2 use global data structure “big_dict”.

class CityDict():
    def __init__():
        self.code_dict = get_code_dict()
    def get_city(city):
        try:
            return self.code_dict[city]
        except Exception:
            return None         

def get_code_dict():
    # initiate code dictionary from file
    return code_dict

def lookup1(city, city_code_dict):
    try:
        return city_code_dict[city]
    except Exception:
        return None

def lookup2(city):
    try:
        return big_dict[city]
    except Exception:
        return None


t = time.time()
d = get_code_dict()
for i in range(0, 1000000):
    lookup1(random.randint(0, 10000), d)

print "lookup1 is %f" % (time.time() - t)


t = time.time()
big_dict = get_code_dict()
for i in range(0, 1000000):
    lookup2(random.randint(0, 1000))
print "lookup2 is %f" % (time.time() - t)


t = time.time()
cd = CityDict() 
for i in range(0, 1000000):
    cd.get_city(str(i))
print "class is %f" % (time.time() - t)

This is the output:

lookup1 is 8.410885
lookup2 is 8.157661
class is 4.525721

So it seems that the two ways are almost the same, and yes, the global variable method is a little bit more efficient.

Edit2:

Added the class version suggested by Amber, and then test the efficiency again. Then we could see from tthe results that Amber is right, we should use the class version.

  • 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-05-19T02:18:43+00:00Added an answer on May 19, 2026 at 2:18 am

    Neither. Use a class, which is specifically designed for grouping functions (methods) with data (members):

    class BigDictLookup(object):
        def __init__(self):
            self.bigdict = build_big_dict() # or some other means of generating it
        def lookup(self):
            # do something with self.bigdict
    
    def main():
        my_bigdict = BigDictLookup()
        # ...
        my_bigdict.lookup()
        # ...
        my_bigdict.lookup()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to use a datetime.strptime on the text which looks like follows. Some
Huge database in mssql2005 with big codebase depending on the structure of this database.
I need to use sed to convert all occurences of ##XXX## to ${XXX} .
I need to use an alias in the WHERE clause, but It keeps telling
I need to use NSImage which appears need to be imported from <AppKit/AppKit.h> .
I need to use a many to many relationship in my project and since
I need to use sendmail from Macs in an office. At the moment, I
I need to use a byte array as a profile property in a website.
I need to use the ReSharper Unit Test Runner to run my MSTest Unit
I need to use lists for my program and needed to decide if I

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.