Collision Handling In Hash Table - Data Structures & Algorithms Tutorials In Python #6

แชร์
ฝัง
  • เผยแพร่เมื่อ 23 ธ.ค. 2024

ความคิดเห็น •

  • @codebasics
    @codebasics  2 ปีที่แล้ว +5

    Do you want to learn python from me with a lot of interactive quizzes, and exercises? Here is my project-based python learning course: codebasics.io/courses/python-for-beginner-and-intermediate-learners

    • @Saurav061
      @Saurav061 8 หลายเดือนก่อน

      Hey, while handling the collisions in __setitem__() function, "if len(element) == 2" why is this necessary?
      Timestamp : 7:42

    • @Random-Sad
      @Random-Sad 5 หลายเดือนก่อน +1

      @@Saurav061 Because we need to make sure that there are more than 1 element in there if we want to return from tuple, otherwise simply return value

  • @aashi9781
    @aashi9781 2 ปีที่แล้ว +5

    Just started on the DSA journey combining practice with your guidance and taking notes. I started my Data science journey around 3n half years back and you were the first tutorial that I clicked for ML. I remember being so naive at that point but later on just got a flow out and got a chance to be a mature professional in data science field. Now feeling the urge for Targeting the Data Structure and Algorithms next!! Thank you so much! Enjoying it!

  • @anshulhedau10
    @anshulhedau10 2 ปีที่แล้ว +3

    I have seen many videos but your videos have simplest explanation among all. Respect.

  • @erenhan
    @erenhan 2 ปีที่แล้ว +2

    this is the best practical exercise and explanation for hashtables for python in entire web, big thank you

  • @RajAryan
    @RajAryan 8 หลายเดือนก่อน +1

    Even to this date your videos are the best on the entire youtube. Hats off to you!! Really enjoying learning DSA :)

  • @deepaksrinivasan8188
    @deepaksrinivasan8188 3 ปีที่แล้ว +5

    @Codebasics, I could image why HashTables are your favorite data structures...:)
    Exercises done and moving onto Stack and Queues ! Just brilliant tutorials.:)

    • @Shourya_performs
      @Shourya_performs 3 ปีที่แล้ว

      greatttttt.
      i wanted to ask a ques dictionary itself handles this collision??

    • @saisridhart3133
      @saisridhart3133 2 ปีที่แล้ว +1

      @@Shourya_performs No bro dictionary overwrites the previous value with the current value

    • @Shourya_performs
      @Shourya_performs 2 ปีที่แล้ว +1

      @@saisridhart3133 yes bro.
      Thnx

  • @johanlopez7452
    @johanlopez7452 2 ปีที่แล้ว +1

    And here I am paying college tuition and students loans to "learn" what this awesome dude gives out for free. You are the best man, love your content.

  • @brijpathak3873
    @brijpathak3873 4 ปีที่แล้ว +10

    You're an excellent teacher, Dhaval. Thanks and hope your passion does a lot of good for you!

  • @heidik1757
    @heidik1757 4 ปีที่แล้ว +10

    Took me a while to really get each of the steps but good video :)

  • @karrianilkumar8123
    @karrianilkumar8123 2 ปีที่แล้ว +1

    You are a very good teacher. Thank you Mr Dhaval

  • @hoatruong2474
    @hoatruong2474 2 ปีที่แล้ว

    Best explanation and implementation of hash table ever, understand it immediately

  • @ShashankData
    @ShashankData 2 ปีที่แล้ว

    Amazing video! This was by far the clearest explanation of this I've ever seen!

  • @ozioma
    @ozioma 4 ปีที่แล้ว +5

    sooo clear and concise!!

  • @svetliodoychinov5580
    @svetliodoychinov5580 2 ปีที่แล้ว +1

    Amazing video! Best explantion I have found and the exercises are very useful I just finnished the nyc_weather one. Used the built in hash function and handles collisions by adding (key, value) tuples to a list that is on the index. Thank you for the great video!

  • @VivekYadav-sy2ec
    @VivekYadav-sy2ec 3 ปีที่แล้ว +1

    Sir your way of teaching is just excellent, god bless you!

    • @codebasics
      @codebasics  3 ปีที่แล้ว +1

      Glad it was helpful!

  • @xidd1
    @xidd1 4 ปีที่แล้ว +2

    This is actually a pretty good revision of the concepts too, thanks

    • @codebasics
      @codebasics  4 ปีที่แล้ว +1

      glad you liked it

  • @NoobTube4148
    @NoobTube4148 2 ปีที่แล้ว +10

    Would be nice if you could touch on double hashing and also explain what happens when hash table itself needs to grow or the individual slot data structure has to grow. What happens to the time complexities in those cases?

    • @EsotericEchoesTailored
      @EsotericEchoesTailored 11 หลายเดือนก่อน +1

      You wrote this question one year ago, I don't know if my answer will help you anymore, but still I'm gonna answer it.
      However big a hash table/ hash map is gonna be, the time complexity will always be O(1), when talking about searching. The downside of hash tables is that if you need to store a huuuge amount of data, you need a pretty complex hash table and it will occupy a pretty big chunk of memory. The time complexity remains the same, though the *space complexity* will increase drastically.

  • @pythonenthusiast9292
    @pythonenthusiast9292 4 ปีที่แล้ว +3

    G.O.A.T you're my hero!!

  • @fluffyisyermom7631
    @fluffyisyermom7631 11 หลายเดือนก่อน

    I'm actually just kind of confused after watching these last two videos. In linked lists you clearly helped us understand the structure of a linked list then asked us to modify what you wrote with our own functions. That was helpful.
    Here you showed us 50 minutes of hash functions and how to mess with lists(arrays) to store values and avoid collisions. Yet none of the excercises have anything to do with the videos themselves.
    > videos: I'm going to teach you how to cook a steak on your stove top
    exercise: now pull out some chicken so we can get started

  • @DispelTV
    @DispelTV 3 ปีที่แล้ว +10

    Great videos, thank you so much! Do you have any projects that use these algorithms and data structures to help us imbed this information into our brains?

  • @ningma1048
    @ningma1048 7 หลายเดือนก่อน

    excellent explanation! thanks

  • @debasismondal3619
    @debasismondal3619 3 ปีที่แล้ว +3

    Thanks a lot Dhaval, I have been learning a lot from you and using this skill to search for a career as a developer or data scientist after 10 years of administration experience, hope I will find a career soon!
    I did all the exercises by myself with your encouragement but I check your every solution as well. I found the solution for Linear Probing is not working as expected when I am running the following case
    t = HashTable()
    t["march 6"] = 20
    t["march 17"] = 88
    del t["march 6"]
    t['march 17']
    However, both of the following is working fine
    def __getitem__(self, key):
    h = self.get_hash(key)
    prob_range = self.get_prob_range(h)
    for prob_index in prob_range:
    element = self.arr[prob_index]
    if element is None:
    continue
    if element[0] == key:
    return element[1]
    def __getitem__(self, key):
    h = self.get_hash(key)
    for i in range(self.MAX):
    element = self.arr[h]
    h = (h+1)%self.MAX
    if element == None:
    continue
    if element[0] == key:
    return element[1]

  • @asahoo550
    @asahoo550 4 ปีที่แล้ว +3

    Can anybody explain since we found the length of tuple is 2 and key is present at 0th index in the list then why do we inserting second key, value pair on that particular index.... isn't it that going to replace the first one

  • @kartikeychhipa3813
    @kartikeychhipa3813 3 ปีที่แล้ว +11

    hey if anyone having a problem understanding for loop in this program try printing idx , element, h in after if statement and append statement

    • @chandran-youtube
      @chandran-youtube 2 ปีที่แล้ว

      in Enumerate function we are not passing the entire array only the element of array ... that is the list of key value pairs of that particular element

  • @priyanshupurohit5431
    @priyanshupurohit5431 2 ปีที่แล้ว

    Thankyou sir i really apreciate your way of teaching

  • @poornimaumapathy9986
    @poornimaumapathy9986 3 ปีที่แล้ว +1

    Crisp and clear tutorial for hash maps sir.Thank you. Could you please explain why if len(element) ==2 is checked in setitem function

    • @bishtman12
      @bishtman12 3 ปีที่แล้ว

      It was done because your element is a (key,value) pair so the len has to be 2 for that.
      Though I think the code will run smoothly without that.

    • @bishtman12
      @bishtman12 3 ปีที่แล้ว +1

      h = self.gethash(key)
      key_exists = False
      slot = self.arr[h] #making a dynamic array to store collisions.
      for index, kv in enumerate(slot): #here Searching if the key already exists or not.
      k, v = kv #splitting the key and value pair
      if key == k: #if it matches the already existing key then insert it in the next slot.
      key_exists = True
      break
      if key_exists: #THIS IS TO UPDATE THE VALUE of same key.
      slot[index] =((key, value))
      else:
      slot.append((key, value))

  • @AngelSanchez-fi7vf
    @AngelSanchez-fi7vf 3 ปีที่แล้ว

    Great video. Thank you!

  • @rupeshchoudhary9237
    @rupeshchoudhary9237 3 ปีที่แล้ว

    Awesome videos. Thank so much for making such precise vidoes.

    • @codebasics
      @codebasics  3 ปีที่แล้ว

      Glad you like them!

  • @rahulgandhi4271
    @rahulgandhi4271 4 ปีที่แล้ว +2

    why did you check the length of element in setitem method ?

  • @Rickyvidere
    @Rickyvidere 3 ปีที่แล้ว +1

    I commend your channel and explanations. Very well put together thank you!

    • @codebasics
      @codebasics  3 ปีที่แล้ว

      I appreciate that!

    • @acoustictuber2227
      @acoustictuber2227 2 ปีที่แล้ว

      @@codebasics sir can u please tell is it oky fir me btech cse interviews. ?

  • @abdullahanati2507
    @abdullahanati2507 2 ปีที่แล้ว

    Great explanation!
    You can use for else in 8:45 min.

  • @pazuzil
    @pazuzil 4 ปีที่แล้ว

    Wow thanks so much. Your explanation was very clear

  • @kamaleshajjarapu3259
    @kamaleshajjarapu3259 4 ปีที่แล้ว +1

    Hi,
    In the below snippet of your Linear Probing code, "if element is None: return" line is not required as it may return None if previous element of the desired element is None while traversing. However it is not running in any case when prob_range (even though it returns you all indexes) is used.
    def __getitem__(self, key):
    h = self.get_hash(key)
    if self.arr[h] is None:
    return
    prob_range = self.get_prob_range(h)
    for prob_index in prob_range:
    element = self.arr[prob_index]
    if element is None:
    return
    if element[0] == key:
    return element[1]
    For better understanding, if I change the code as shown below , then it will return None if previous element is None before the desired element is executed.
    Eg: [('march 17', 30), ('march 163', 355), ('march 193', 455), ('march 443', 455), ('march 553', 455), ('march 123', 455), ('march 133', 355), None, None, ('march 6', 20)]
    Output: It returns None
    for ''march 6'
    def __getitem__(self, key):
    h = self.get_hash(key)
    if self.arr[h] is None:
    return
    ##prob_range = self.get_prob_range(h)
    for prob_index in range(len(self.arr)):
    #Change here
    element = self.arr[prob_index]
    if element is None:
    return
    None #Change here
    if element[0] == key:
    return element[1]
    Thanks

  • @arulsood8799
    @arulsood8799 2 หลายเดือนก่อน

    There is no linked list in the implementation of this program as you have not implemented one. Please add correction note. (You speak of linked list at around the 5:45 mark.)

  • @shubhz.sayzzz
    @shubhz.sayzzz 3 ปีที่แล้ว +2

    Instead of appending (key, value) tuple, can we append a list [key,value]? What changes will it make?

    • @usamanadeem8504
      @usamanadeem8504 3 ปีที่แล้ว

      bruh first thing you should know is that we can't update a tuple so, he was not appending tuple he was appending list!!!

  • @zd676
    @zd676 4 ปีที่แล้ว +4

    Great video! However I do have a confusion as you call a list of tuples as linked list in the video. Shouldn't it be just an array of tuples? Since these tuples are not really linked in anyway. I mean if I want to insert another tuple or delete one tuple, the time complexity here is O(n), where as it should be O(1) for linked list, correct?

  • @chuanzong7904
    @chuanzong7904 4 ปีที่แล้ว +3

    for the setitem function, is the 'len(element) == 2 ' necessary? the every element in the linklist is the key-value pair, therefore it must be 2?

    • @jimyi2305
      @jimyi2305 4 ปีที่แล้ว

      yea i also didnt get it can someone explain?

    • @ultronhack8151
      @ultronhack8151 3 ปีที่แล้ว

      Its not required

    • @likhithdasari7794
      @likhithdasari7794 3 ปีที่แล้ว

      yeah it may not be useful

  • @HarshSingh-lp5xo
    @HarshSingh-lp5xo 3 ปีที่แล้ว +3

    sir, I have a doubt !!! why you saying that list as link-list at index h, as it is working as a list, not link-list.

  • @perfectcell1157
    @perfectcell1157 3 หลายเดือนก่อน +1

    In the __setitem__ method why do we have to check if the length of the element array is two ?
    other than that great tutorial ♥️

  • @pieropanariello8959
    @pieropanariello8959 4 ปีที่แล้ว

    Thanks so much! Keep making these vids!

  • @nityanandk5522
    @nityanandk5522 3 ปีที่แล้ว +3

    why the len(element) == 2 condition in the __setitem__ function

    • @KarinaRodriguez-yv7mf
      @KarinaRodriguez-yv7mf 3 ปีที่แล้ว

      I had the same question: I think its because if you notice he initiates the array as a list of empty lists. Python will return true to the if -statement in question for an empty list but we only want it to return true if there exist a tuple at each element in linked list. Seems to me hes being overcautious for the case that someone does want to insert an empty list as the key to a specific value.

    • @nityanandk5522
      @nityanandk5522 3 ปีที่แล้ว

      @@KarinaRodriguez-yv7mf its tuple of two elements ie key and value that's why len(element)==2

  • @AdityaGupta-xp7ov
    @AdityaGupta-xp7ov 9 หลายเดือนก่อน

    Can't we use hash function which is readily available and gives the unique hash value for every input so we won't be needing chaining and there won't be any case where the index of two or more different keys will be matching with each other.

  • @Pookie1998-x9h
    @Pookie1998-x9h 2 ปีที่แล้ว +1

    Thanks for the great explanation......but I'm getting h value for 'march 6' and 'march 17' is different that is 9 and 59 ...so how to resolve this issue?

    • @somilagrawal8172
      @somilagrawal8172 2 ปีที่แล้ว

      It's bcz you are using mod 100(means a 100 length array) but he is(in this video) using mod 10( reduced the array size to 10). In case you don't understand mod function here is used to make sure that our no.(index in this case) stays within our defined range.

  • @mandihaase2744
    @mandihaase2744 4 ปีที่แล้ว +12

    Thank you so much for your video! How would you implement a rehash to resize the current hash table? Any suggestions you could provide would be greatly appreciated!

    • @cyborgoni
      @cyborgoni 3 ปีที่แล้ว

      I second this. I'm going through this transactional data that needs data augmentation with hash function and what Mandi has requested, will help alot. Thanks!

  • @apoorvshrivastava3544
    @apoorvshrivastava3544 4 ปีที่แล้ว +1

    Sir Sunday aur morning dono good ho gaye

  • @deepak-georgethomas3152
    @deepak-georgethomas3152 4 ปีที่แล้ว

    This was really helpful. Thank you.

  • @prithvijali2629
    @prithvijali2629 2 ปีที่แล้ว +2

    Hi
    i have a question here, to avoid collision we can resize the hashtable or increment it by one. So that we can get different hash values if there are any collisions. Is it possible to change the size of hashtable dynamically?

    • @davejenil1537
      @davejenil1537 2 ปีที่แล้ว

      yes possible but again problem of re-allocation if you try to insert beyond the capacity

  • @akshaynitin5589
    @akshaynitin5589 5 หลายเดือนก่อน

    i have doubt, for this setitem, getitem, delitem, still we are dependent on O(n). but how we are telling this o(1) ?

  • @sumitkumarsah8782
    @sumitkumarsah8782 4 ปีที่แล้ว +3

    Sir can you please make videos on dynamic programming...or can you please suggest some good channels from where i can follow.

  • @sachinmaurya3259
    @sachinmaurya3259 3 ปีที่แล้ว

    very informative

    • @codebasics
      @codebasics  3 ปีที่แล้ว

      Glad it was helpful!

  • @SARATHBABU-ni4pt
    @SARATHBABU-ni4pt 7 หลายเดือนก่อน

    Can you explain linearprobing imlementation same way??

  • @vidhansaini9296
    @vidhansaini9296 5 หลายเดือนก่อน

    is this implementation is just for explaning the concept of hash table or it's practical cause why make thing when we already have dictionary and other data structures working like a hash table.

  • @jayshreedonga2833
    @jayshreedonga2833 2 ปีที่แล้ว

    thanks nice session

  • @siddharthsinghh
    @siddharthsinghh 3 ปีที่แล้ว

    Room looks clean

  • @maggiezhang145
    @maggiezhang145 2 ปีที่แล้ว

    How come I don't see a Linked List? I only see the array was used in the collision place?

  • @paulclouseau7998
    @paulclouseau7998 4 ปีที่แล้ว

    for adding elements to the linked list just do append method, you dont need to do the enumerate loop and the found check

    • @paulclouseau7998
      @paulclouseau7998 4 ปีที่แล้ว

      I thinkkkkkkk.... :(((((

    • @codebasics
      @codebasics  4 ปีที่แล้ว

      You need to do find check. Else if some one is updating a value at a given key it won't work

  • @dikshantmadaan9116
    @dikshantmadaan9116 ปีที่แล้ว

    I am using following function:
    def add(key, val):
    hash_value = get_hash(key)
    d[hash_value].append((key, val))
    instead of your __setitem__() function and still my code is working fine, and I don't understand why?
    and when I am using your __setitem__(), I am getting an error on the following line:
    self.arr[h][idx] = (key,val)

  • @ducanhnguyen6903
    @ducanhnguyen6903 ปีที่แล้ว

    You said "chaining" is using linked list; but I only see adding tuples to a list. Where is the linked list? Am I missing something? Thanks!

  • @ashaypatil
    @ashaypatil 2 ปีที่แล้ว

    def __delitem__(self, key):
    h = self.get_hash(key)
    for idx, k in enumerate(self.arr[h]):
    if k[0] == key:
    self.arr[h].remove(self.arr[h][idx])

  • @Datatalksbro
    @Datatalksbro 2 หลายเดือนก่อน

    def display(self):
    for index, node in enumerate(self.arr):
    if node is not None:
    chain = []
    current = node
    while current is not None:
    chain.append(f'({current.key}: {current.value})')
    current = current.next
    print(f'Index {index}: -> {" -> ".join(chain)}')
    else:
    print(f'Index {index}: -> None')

  • @massefromla2554
    @massefromla2554 ปีที่แล้ว

    I can not find your exercises anywhere on-site . No Datastructure folder . Where are .cvs files .

  • @amanpandey9405
    @amanpandey9405 4 ปีที่แล้ว +1

    whats the use of len(element)==2 in " if len(element)==2 and element[0] == key" ???

    • @techno-tronics4946
      @techno-tronics4946 4 ปีที่แล้ว +2

      Because we are checking if the key value pair already exists or not. If they exit then just update them by new key,val pair else append them to the same index.
      Here's a Simpler code without found variable:
      hash_index = self.get_hash(key)
      for idx,ele in enumerate(self.hash_map[hash_index]):
      if ele[0]==key and len(ele)==2:
      self.hash_map[hash_index][idx] = (key,value)
      break
      else:
      self.hash_map[hash_index].append((key,value))

    • @zestful14
      @zestful14 4 ปีที่แล้ว

      @@techno-tronics4946 I was confused about why we needed `self.hash_map[hash_index][idx] = (key,value)` since the if statement already checks for the key, value pairs. But now I understand that line of code is there to update a value, not just check it. Thanks for the explanation!

    • @techno-tronics4946
      @techno-tronics4946 4 ปีที่แล้ว

      @@zestful14 Anytime Bruh. Keep Learning 😁😁

    • @amrabdelatyfathallah2487
      @amrabdelatyfathallah2487 9 หลายเดือนก่อน

      i think len(element) == 2 not important , because i want to check if key found in this position or not if key already found i will update its value regardless it was key, value or key without value i will put value for it , if it has length one that means key , None value that mean key already exist but has no value so i will update its value so i think len(element) == 2 not important@@techno-tronics4946

  • @Tenacioustarantula
    @Tenacioustarantula 3 ปีที่แล้ว +1

    Isn't the following incorrect?it always replaces the tuple in index 0 and it doesn't append

  • @deeprajmazumder6261
    @deeprajmazumder6261 2 ปีที่แล้ว

    can anyone explain whats happening in 7:51

  • @abhi-_-
    @abhi-_- 3 ปีที่แล้ว

    what would be different if a dictionary was used in place of a simple list for chaining

  • @jykw1717
    @jykw1717 4 ปีที่แล้ว

    Sir you're awesome. I subscribed and liked your videos. In the future please make videos on how to prepare for coding test and interviews and tech interviews for companies

  • @TheBhartiyaTrainee
    @TheBhartiyaTrainee 3 ปีที่แล้ว

    Let me take the chance to click the 1000th like!! Do we have to manually deal with csv files or can the third party/standard lib modules be used?

    • @codebasics
      @codebasics  3 ปีที่แล้ว +1

      well csv files can be read and analyzed easily using a python module called pandas

  • @chenarrzgar4409
    @chenarrzgar4409 4 หลายเดือนก่อน

    hello sir, thank you for your great tutorial, I’ve been following your AI engineer roadmap, so far I have learned a great deal of knowledge from your videos, I really appreciate it, but here I have a problem, the march 6 and march 17 with me return two different hashes which is 9 and 59 respectively, I’m kinda confused whether there is an issue with my code or you just made a simplifying assumption for understanding?

    • @Danicious.
      @Danicious. 4 หลายเดือนก่อน

      Same issue
      When he was explaining Collision handling he assigned MAX to 10 instead of 100, that solved it for me and I think it will for you too

  • @ashaypatil
    @ashaypatil 2 ปีที่แล้ว

    Could someone please tell me why are we not appending the key value pair whenever we find key to be already present.
    We should append the key value pair to that element, isn't it?
    def __setitem__(self, key, value):
    h = self.get_hash(key)
    found = False
    # if key exists already then append the value to the key
    for idx, k in enumerate(self.arr[h]):
    if len(k) == 2 and k[0] == key:
    self.arr[h].append((key, value))
    found = True
    break
    # if the key value pair doesn't exist, add the key and value
    if not found:
    self.arr[h].append((key, value))

  • @vasilijejukic9682
    @vasilijejukic9682 4 ปีที่แล้ว

    Thanks ! I did 2 exercises by myself :) Soon going to Stack and Queue !

    • @codebasics
      @codebasics  4 ปีที่แล้ว +1

      👍☺️ good job

  • @debasmitachoudhury7398
    @debasmitachoudhury7398 4 ปีที่แล้ว +1

    Sir, your video was of great help. Thank you. However, I have a doubt it would be helpful if you please clarify it. In chaining as said in video linked list is used. As I know every node in LL has a head and a next. So, if LL is used here then don't we have to use head and next in set and del methods? or simple list has been used here?

    • @codebasics
      @codebasics  4 ปีที่แล้ว +1

      In python a simple list is a dynamic array. One can use dynamic array as well in place of a linked list. In previous tutorials I have mentioned difference between dynamic and static array. Please go through that.

    • @GeorgeNyamao
      @GeorgeNyamao 4 ปีที่แล้ว

      A simple list is sufficient for chaining. I suspect chaining is used to show various ways of avoiding collision. This is the brute force solution. Then we go to better rehashing solutions like linear probing, quadratic probing, and eventually using the python built-in hashtable.

  • @saramsasherchan3731
    @saramsasherchan3731 3 ปีที่แล้ว +1

    Sir i am getting march 17 hash as 59 is it wrong or its related ASCII code according to any other configuration of the computer system

    • @vishwas2764
      @vishwas2764 3 ปีที่แล้ว +1

      Try using "July 18" and "July 27" it worked out for me!

    • @pup_lover
      @pup_lover 2 ปีที่แล้ว

      @@vishwas2764 thx! you found it out by trial and error? because it would take a long time to find 2 keys with same hash values

  • @sanjays395
    @sanjays395 4 ปีที่แล้ว +1

    The case where all the locations are filled in #LinearProbing i.e., 10 elements are filled according to the example what we need to do?

  • @vigneshm7462
    @vigneshm7462 3 ปีที่แล้ว

    Which hash function does dictionary implements behind is it linear probing or chaining using list

  • @55mayakeren55
    @55mayakeren55 2 ปีที่แล้ว

    why not using dict element in the self.arr instead of linked lists?

    • @anuvind_m
      @anuvind_m ปีที่แล้ว

      Python dictionary works on the idea of hash table. Here we are building our own version of dictionary. So, using python's inbuilt dictionary to create a user defined dictionary makes no sense.
      I know your comment is from 9 months ago, may my comment be useful for upcoming viewers.

  • @KANISHKSAHUSIME
    @KANISHKSAHUSIME 2 ปีที่แล้ว

    hello sir instead of using for loop directly if we check number of elements present in each list then going to for loop will it decrease the time complexity?

  • @sergioorozco7331
    @sergioorozco7331 2 ปีที่แล้ว

    Awesome content! In my opinion, the following:
    for index,key_val_pair in enumerate(self.arr[h]):
    ## find key_value_pair that matches the key passed in by the function caller
    if len(key_val_pair) == 2 and key_val_pair[0] == key:
    self.arr[h][index] = (key,value) #tuples are immutable, so just replace the tuple with an entire new one
    found = True
    break
    could be replaced with:
    for key_val_pair in self.arr[h]:
    if len(key_val_pair) == 2 and key_val_pair[0] == key:
    key_val_pair = (key,value)
    found = True
    break
    It's shorter, and I think it's more intuitive. Thanks for these videos!

    • @nononnomonohjghdgdshrsrhsjgd
      @nononnomonohjghdgdshrsrhsjgd 2 ปีที่แล้ว

      it doesn't update the values. I can not tell why, but I have just tried these loops on this array:
      arr = [[(i,i),(2*i,2*i)] for i in range(10)]
      your version doesn't update:
      for k in arr[9]:
      k=("value","key")
      this gives error:
      for k in arr[9]:
      k[0]="v"
      k[1]="k"
      this doesn't update:
      for i,k in enumerate(arr[9]):
      k=["key","val"]
      this updates the values:
      for i,k in enumerate(arr[9]):
      arr[9][i]=["key","val"]
      this updates as well:
      for i,k in enumerate(arr[9]):
      arr[9][i]=("key","val")

  • @adbuth4854
    @adbuth4854 3 ปีที่แล้ว

    hello sir, what does the idx represents while iterating through the llist

  • @陳寬-x6d
    @陳寬-x6d 2 ปีที่แล้ว

    great channel sir. Can you please make vedios on priority queue and heap data structure?

  • @tanyipeng3440
    @tanyipeng3440 4 ปีที่แล้ว

    Why is the hashtable in python implement as nested list however , other languages use a array that is linked with linked list to store data that has collisions?

  • @meralmaradia4774
    @meralmaradia4774 2 ปีที่แล้ว

    Hello Sir, can you please create a video developing of project using only DSA ?

  • @swaniketchowdhury
    @swaniketchowdhury 3 ปีที่แล้ว

    Just a quick question: If Collision Handling methods make the search runtime go up to O(n), then is it really worth it to use hashmaps in the first place? I believe the lookup time also won't be constant time, it should be a linear time in the worst case.... Correct me if I'm wrong!

    • @thammayyaarava2259
      @thammayyaarava2259 3 ปีที่แล้ว

      Yeah it will be in the first place if we wrote an efficient hash function..

  • @sourabhwadhwa05
    @sourabhwadhwa05 4 ปีที่แล้ว

    I have one question, lets say we have three columns in an excel and i want to print the third column value on the basis of two columns as key. Ex: ice and cream keys give value “ice-cream trucks” another ex: ice and water gives “cold water” as value another ex: water and cream gives “yuck taste” as value , assume this in a excel and i want to display ice cream truck or yuck water or cold water on the basis of two key inputs how can i do that!!

  • @TKZntkx
    @TKZntkx 2 ปีที่แล้ว

    __setitem__ has a bug on python 3.11.0, the value cannot be set

  • @yashpreetkaur2042
    @yashpreetkaur2042 2 ปีที่แล้ว

    I understood the video well but the exercises are like made me feel am I really understanding. Please someone help me to choose the right path, do I need to go for learning advanced python side by side?

  • @АлександрЮсько-у2д
    @АлександрЮсько-у2д 4 ปีที่แล้ว

    Thank you so much

  • @guptalovelymananishu
    @guptalovelymananishu 4 ปีที่แล้ว

    Dhaval aap apni har video mai bol diya kijiye ki apka hindi channel bhi hai jo hindi mai comfortable hai woh bhi same benefits le sakta hai . Yeh ek request hai na ki salah. You are doing great job.

    • @codebasics
      @codebasics  4 ปีที่แล้ว +1

      Sure neha but I don't have many tutorials on Hindi channel. That's why I am not highlighting

  • @urrahman196
    @urrahman196 3 ปีที่แล้ว

    Instead of taking python list what if I take a dictionary to store? It would not require looping all the values to find an item in that cell

    • @abhi-_-
      @abhi-_- 3 ปีที่แล้ว

      is it possible to place a dictionary(a hash) under another hash table?

  • @SwagatSusmoy
    @SwagatSusmoy 3 ปีที่แล้ว

    Hey man Excellent explanation and am studying from your videos right now
    I just found one small error in the hash table with linear probing solution
    We know according to our hash function 'march 6' and 'march 17' give the same value ie. 9
    Now when using probing once we insert the value of march 6 followed by march 17 and if we delete the value for march 6 then calling for the value of march 17 returns none as while checking for the hash index it finds the index empty and returns none. Would love it if you could correct it as to how to get around this problem.
    Thanks a lot for the videos and seeing this in advance

  • @gloryasuquo5043
    @gloryasuquo5043 4 ปีที่แล้ว +1

    Hello Sir. Thank you so much for your videos, it has really been of help. I have just worked on the linear probing exercise and my solution is a little different from yours but it does what its suppose to do. I was hoping you could look into mine and comment on it, if it's right and why you didn't work it this way.
    class HashTable:
    def __init__(self):
    self.MAX = 10
    self.arr = [None for i in range(self.MAX)]
    def get_hash(self, key):
    h = 0
    for char in key:
    h += ord(char)
    return h % self.MAX
    def __setitem__(self, key, value):
    h = self.get_hash(key)
    if self.arr[h] is None:
    self.arr[h] = (key, value)
    else:
    empty_space = self.arr.index(None)
    # index() finds the first occurrence of an empty space
    self.arr[empty_space] = (key, value)
    def __getitem__(self, key):
    h = self.get_hash(key)
    return self.arr[h]
    h = HashTable()
    h['march 6'] = 130
    h['march 7'] = 78
    h['march 8'] = 210
    h['march 9'] = 530
    h['march 17'] = 28
    print(h.arr)
    print(h['march 7'])
    Thank you very much sir. I look forward to your reply.

    • @akashvshroff
      @akashvshroff 4 ปีที่แล้ว +1

      Hey, so when you manually modify your index position based on whichever the closest empty space is, you only do that in the setitem method and there is no conceivable way to calculate this changed index in the delitem and getitem methods so you end up with the wrong answer!
      For example: suppose you have an array the size of 5, and 2 keys: 'x' and 'y' which give you the same index 2. First when the array is empty, you add the value associated with 'x', say 'foo', at index 2. Now when you try to add 'boo' associated with key 'y', you get the same index 2 but then you see that it is not empty and so you add it at index 0. Now your hash function doesn't know that you've changed the index for the key 'y' to 0 and so when the user tries accessing the hash table with ['y'], the index that the function getitem receives is still 2 and so it returns the value of key 'x' instead of key 'y', thereby returning 'foo' instead of 'boo'! I hope this helped :)

    • @debasmitachoudhury7398
      @debasmitachoudhury7398 4 ปีที่แล้ว

      @@akashvshroff HI.. can you kindly help in my doubt. In chaining as said in video linked list is used. As I know every node in LL has a head and a next. So, if LL is used here then don't we have to use head and next in set and del methods? or simple list has been used here?

    • @akashvshroff
      @akashvshroff 4 ปีที่แล้ว

      @@debasmitachoudhury7398 Hey, so you could use your linked list and do that but here to make the implementation slightly easier to understand and for simplicity in code, a python list has been used. Moreover, unlike other languages, a python list is dynamic which means that it can be extended with no initial size and so it seems fitting for this scenario :)

    • @jerrygeorge180
      @jerrygeorge180 2 ปีที่แล้ว

      @@debasmitachoudhury7398 a simple list have been used

  • @chandannooli9218
    @chandannooli9218 4 ปีที่แล้ว

    My IDLE compiler is asking me to define f

  • @zap7140
    @zap7140 3 ปีที่แล้ว

    Thanks

  • @LtMewS
    @LtMewS 4 ปีที่แล้ว

    Is that linked Liston separate chaining or usual 2d array

  • @cyclogenisis
    @cyclogenisis 2 ปีที่แล้ว

    You didn't mention something pretty important as of 3.7 which is ordered insertion.

  • @TK-vt3ep
    @TK-vt3ep 4 ปีที่แล้ว

    Hello Sir,
    Since we already we have dict handy in python, then why we exactly use hashmap? Please help me to understand. Is it just to get to know how dict works

    • @codebasics
      @codebasics  4 ปีที่แล้ว +1

      Yes. It is just to understand how dict works internally 👍😊

  • @rrvv01
    @rrvv01 3 ปีที่แล้ว

    Keys 'march 9' and 'march 10' has the same h, but are different keys, how do we deal with that. Thanks :-)

  • @AbhishekKumar-dg2js
    @AbhishekKumar-dg2js 4 ปีที่แล้ว

    why are we using enumerate() , what will happen if we donot use that ?

    • @pythonenthusiast9292
      @pythonenthusiast9292 4 ปีที่แล้ว

      just to get the index so that we can insert the element in that particular index of the subarray of the array

  • @DiasDenny
    @DiasDenny 4 ปีที่แล้ว

    Sir,I have a doubt.Suppose all the elements got collided .Won't it append all the elements in a single array and therefore Won't the searching time be o(n)

    • @codebasics
      @codebasics  4 ปีที่แล้ว +1

      It can happen. And for that reason your hash function should be designed in a way that it can distribute elements equally among all buckets

  • @Bobby-vv1sn
    @Bobby-vv1sn 8 หลายเดือนก่อน

    9:12 why len(element) == 2

    • @charlassyt
      @charlassyt 4 หลายเดือนก่อน

      Whether to check the current element has only two items

  • @jaheerkalanthar816
    @jaheerkalanthar816 4 ปีที่แล้ว

    please can anyone explain,what is the line means
    def get_prob_range(self, index):
    return [*range(index, len(self.arr))] + [*range(0,index)]

    • @AlexDeathway
      @AlexDeathway 4 ปีที่แล้ว

      It return list of all number from index to end + start to index.
      for example your element key is 7 and hashmap length is 10, then this code will return
      [7, 8, 9, 10, 0, 1, 2, 3, 4, 5]
      Now this list can be used for iteration.