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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T15:47:19+00:00 2026-05-28T15:47:19+00:00

I was messing around with making a command line parser and was wondering what

  • 0

I was messing around with making a command line parser and was wondering what kind of hash algorithm python dict’s use?

The way I have it set up, I have a pattern match algorithm which matches tokenized input sequences with a dictionary key. Some of the keys are relatively long (length 5 or 6 tuples of 6-7 character strings). I was wondering if there was a point at which long dictionary keys significantly reduce the efficiency of key retrieval.

  • 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-28T15:47:20+00:00Added an answer on May 28, 2026 at 3:47 pm

    The hash that it uses depends on the object being used as a key — each class can define its own __hash__() method, and the value that it returns for a particular instance is what is used for the dictionary.

    Python itself provides the hash implementation for str and tuple types. A quick look at the source should reveal the exact algorithm for those.

    The hash of a tuple is based on the hashes of its contents. The algorithm is essentially this (simplified slightly):

    def hash(tuple):
        mult = 1000003
        x = 0x345678
        for index, item in enumerate(tuple):
            x = ((x ^ hash(item)) * mult) & (1<<32)
            mult += (82520 + (len(tuple)-index)*2)
        return x + 97531
    

    For strings, the interpreter also iterates over every character, combining them with this (again, slightly simplified) algorithm:

    def hash(string):
        x = string[0] << 7
        for chr in string[1:]:
            x = ((1000003 * x) ^ chr) & (1<<32)
        return x
    

    A bigger issue to worry about is avoiding hash collisions. Colliding hash keys will cause a linear search as the dictionary tries to find a place to store the new object (this is now being recognized as a security issue, and tha behavior may be changing in upcoming python versions)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am messing around with C# and am making a prototype GUI (with no
Whilst messing around with some x86 asm, I got to wondering about cases where
After messing around a little with making a GUI with the Windows API, I
Recently I've been messing around a fair amount with OpenGL, and I have come
Background info: Over the past week, I have been messing around with Chrome extensions
Messing around with 'extension functions' for the List module. (I spent quite a while
I am messing around with a toy interpreter in Java and I was considering
I was messing around with RhinoMocks this morning and couldn't run my tests because
I am messing around with Doctrine (version 1.0.3) to see if would make a
I'm messing around with some C code using floats, and I'm getting 1.#INF00, -1.#IND00

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.