Build a Hash Table - Build a Hash Table

Tell us what’s happening:

Part 11 and 17 isn’t getting solved. I know it is saying not to delete the entire dictionary but I don’t understand how is it deleting the entire if I am only deleting at a certain key and also part 17, i dont know what to do

Your code so far

class HashTable:
    def __init__(self):
        self.collection = {}
    def hash(self, key):
        if not isinstance(key, str):
            raise ValueError('Key must be a string')
        hash_sum = 0
        for char in key:
            hash_sum += ord(char)
        return hash_sum
    def add(self, key, value):
        hash_value = self.hash(key)
        if hash_value not in self.collection:
            self.collection[hash_value] = {}
            self.collection[hash_value][key] = value
        else:
            self.collection[hash_value][key] = value
        return self.collection[hash_value][key]
    def remove(self,key):
        hash_value = hash(key)
        if hash_value in self.collection:
            if key in self.collection[hash_value]:
             del self.collection[hash_value][key]
        return
    def lookup(self,key):
        hash_value = self.hash(key)
        if hash_value in self.collection and key in self.collection[hash_value]:
         return self.collection[hash_value][key]
        return None
print(HashTable().add('dear', 'friend'))
print(HashTable().add('read', 'book'))
print(HashTable().hash('read'))
print(HashTable().lookup('dear'))

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/145.0.0.0 Safari/537.36

Challenge Information:

Build a Hash Table - Build a Hash Table

Testing like this

q = HashTable()
q.add('dear', 102)
q.add('read', 101)

print('before', q.collection)

q.remove('dear')

print('after ', q.collection)

it seems that the issue is the opposite one, your remove is actually not removing

I am not able to figure out how to remove. I understood now it is not removing but I cannot understand why the del is not deleting the value

try to look up if you can actually use in like that

Thank you, I got it now.