Search code examples
pythonhashtabletypeerrornonetype

TypeError: list indices must be integers or slices, not NoneType


I have some troubles with the Type Error (TypeError: list indices must be integers or slices, not NoneType)

I just want to do a little hash table with linear probing in python. Therefore I used to make an insert method, a find method and a hash method so far. I have problems with my insert method. This is my code

    def insert(self, key):

        index = self.hash(key)
        count = 0
        if self.table[index] == None or self.table[index].getValue() == "DELETED":
            self.table[index] = key
            return count + 1
        else:
            inserted = False
            while inserted != True:
                index += 1
                count += 1
                if index == self.size:
                    index = 0
                if self.table[index] == None or self.table[index].getValue() == "DELETED":
                    self.table[index] = key
                    return count

The problem are these two lines

if self.table[index] == None or self.table[index].getValue() == "DELETED":

What should I do? I need to compare my table index with None. Has anybody an idea?


Solution

  • It looks like your self.hash function is returning None. Since the value of index is None it is not able to find the value in list self.table. Can you write the hash function as well?