gsamaras gsamaras - 5 months ago 6x
Python Question

Towards understanding dictionaries

I am required to use multiple hashtables, so in , I would normally use an std::unordered_map. So far I can understand that I can use a dictionary in Python, so let's assume the following code:

my_dict_1 = {}
my_dict_1['foo'] = 1
my_dict_2 = {}
my_dict_2['foo'] = 2

Will the two dictionaries be using different hash functions (notice that the key is the same), thus they can be considered two different hash tables (I mean that they will actually store the data differently)?


Yes the dictionaries are two different objects of course, but the question is about the technique that they will use to store the data!


A simple Python shell experiment to show that different dictionaries can use the same key:

>>> my_dict_1 = {'foo':1}
>>> my_dict_2 = {'foo':2}
>>> my_dict_1,my_dict_2
({'foo': 1}, {'foo': 2})

This is a good discussion of how it is implemented. The key point is that each dictionary is allocated its own portion of memory (which can of course grow as needed). The exact same hash function is used for both dictionaries, but is being used to probe different areas in memory.