2

I was watching a video on Hash Table, and the professor clearly said: enter image description here

Now suppose I am using Python to add million values into my hash table.

Code:

dictionary = {}
for i in xrange(1000000):
    dictionary[i] = ''

dictionary = {}
for i in ['A', 'B', 1, 2, 3, 4, 1, 'Hi']:
    dictionary[i] = ''

How do you calculate a hash function here? Since you are constantly adding elements into the dictionary, does the hash function continuously change based on total number of elements? Or The hash function is decided once before inserting all the elements?

Since my program doesn't know what all values might come in the dictionary, how do we decide the hash function here? My list of values can be anything here not just numbers.

4
  • 1
    Which hash function are you referring to, that of the class that you'll be using as the key to your table, or of the hash table class itself? Commented Dec 6, 2015 at 3:25
  • The one that can magically generate a unique key for each value in my list. Commented Dec 6, 2015 at 3:27
  • Hash functions don't do that. Commented Dec 6, 2015 at 3:35
  • Could you elaborate more ! Commented Dec 6, 2015 at 3:36

1 Answer 1

2

Keys for Dictionaries in Python can be an immutable data type, so long as it supports hashing. As an example,

X = 1
print(X.__hash__())

This shows the unique hash value for the value of this object. Each key within a Dictionary MUST be unique, that's why you cannot have duplicate keys.

Y = "1"
print(Y.__hash__())

All hash values within Python are integral, irregardless of the data type you're hashing.

Sign up to request clarification or add additional context in comments.

6 Comments

Indent your lines 4 extra spaces for code formatting.
I was fighting it on my iPad, logged into my Macbook ~ Thanks @user2357112
Can you say that two different values will not generate the same hash?
There must be some function hash function which is generating all these integral value right?
Check out this link: docs.python.org/2/reference/datamodel.html - They talk about Custom Hash functions and other related things
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.