## Hash Table – Data Structures & Algorithms Tutorials In Python #5

Hash map or hash table is a very popular data structure. It allows to store key, value pairs and using key you can locate a value in O(1) or constant time. We will implement simple hash table in python in this tutorial and in part 2 we will see how to handle collisions.
Code: https://github.com/codebasics/data-structures-algorithms-python/tree/master/data_structures/4_HashTable/4_HashTable.ipynb

Topics
00:00 Introduction
00:34 Array vs hashmap Scenario
03:52 Memory presentation (array vs hashmap)
04:15 Hash function
07:09 Big O Analysis
07:17 Hashmap in Python/JAVA/C++
07:47 Implement Hash Table in python

#datastructures #algorithms #python

Website: http://codebasicshub.com/
Patreon: https://www.patreon.com/codebasics

Source

### Comment List

• codebasics
January 22, 2021
• codebasics
January 22, 2021

i have a question w/ hashmaps/tables, in your example of using h = 0 and h += ord(char), and h % 100, isn't it possible for multiple occurrences of different h values getting the same h % 100, for example h = 309, 209, 109 all equal h % 100 = 9, additionally I would imagine there are multiple waysof getting the sum_total h = 309 depending on the characters, wouldn't these multiple occurrences overwrite each other in the hash table?

• codebasics
January 22, 2021

Thank you ðŸ’• sir ðŸ˜Š

• codebasics
January 22, 2021

very well explained…
Thank you sir

• codebasics
January 22, 2021

You have been incredibly helpful to me and many others. Thank you!

• codebasics
January 22, 2021

Hello, That was really a nice illustration. I have a question if you could help me with that…I have to store binary matrices and further, I want to retrieve that matrices on some similarity(exact or approximate) basis with the query matrix, not on the index basis. Can you suggest a suitable data structure for this task? I mean should I use the Hash-table or Tree or any user-defined data structure?

• codebasics
January 22, 2021

But how to cook lasagna?

• codebasics
January 22, 2021

This video is amazing thank you

• codebasics
January 22, 2021

so writing into a dictionary or writing into list which is more complex by time

• codebasics
January 22, 2021

amazing video thank you so so much

• codebasics
January 22, 2021

Dude, you are cool and your videos are awesome. Best wishes from NYC.

• codebasics
January 22, 2021

thumbs up

• codebasics
January 22, 2021

finally i got the concept of hash table. thank you so much.

• codebasics
January 22, 2021

Can you please share how the retrieving function is changing when the add function is changed to __getitem__?

• codebasics
January 22, 2021

Hey, why do we need to set the size of the hash table? Can't we change it dynamically?

• codebasics
January 22, 2021

You're amazing dude!

• codebasics
January 22, 2021
• codebasics
January 22, 2021

I have been python developer for past 4 years.
Just used dictionary many times. But never knew how the internals of it worked.
This was great explanation. Thanks.

• codebasics
January 22, 2021

Okay few questions:
1. Where is the hash table stored?
2. Are these objects(key, value) pairs stored contiguously in memory?
3. If my self.arr has 5 elements and I try to add the 6th one the first added hash-value is overwritten in self.arr but I can still access the new value with the first added hash, why is that so?

• codebasics
January 22, 2021

@codebasics.. I really like and Appreciate your effort and i understand much better now.. thank you for that… while i have a qq:

Q: what if the modulo returns a value which is out of the 100 (MAX) like 101.. or some thing..? i mean if ord(char) returns a value which is out of bounds..?

• codebasics
January 22, 2021

8:10 , why not use the hash function instead of ord(). Are there trade offs for using either?