When adding an element, throws it into all lists from hash_table. I sat in the pythontutor for about an hour, and could not understand why everything, if I refer to the list of hash_table by index. Maybe I need a fresh look ...
class HashTable: def __init__(self, hash_type, values): self.hash_type = str(hash_type) self.hash_types = {'1': self.hash_type_1, '2': self.hash_type_2, '3': self.hash_type_3, '4': self.hash_type_4, '5': self.hash_type_5} self.values = values self.hash_table = [LinkedList()] * len(self.values)*3 print(self.hash_table) self.hash_types[self.hash_type]() def hash_type_1(self): for item in self.values: place = item % 12 self.hash_table[place].add(item) def hash_type_2(self): pass def hash_type_3(self): pass def hash_type_4(self): pass def hash_type_5(self): pass def print_hash_table(self): print(self.hash_table) def __str__(self): str_1 = '' for items in self.hash_table: if len(items) != 0: for item in items: str_1 += str(item) str_1 += ' ' str_1 += '\n' return str_1 class Node: def __init__(self, data): self.nxt = None self.data = data def __set__(self, value): self.nxt = value def __repr__(self): return str(self.data) def __str__(self): return str(self.data) class LinkedList: def __init__(self): self.lst = list() self.index = 0 def add(self, other): cur_node = Node(other) if len(self.lst) == 0: self.lst.append(cur_node) self.index = 0 else: self.lst.append(Node(other)) self.lst[self.index].nxt = cur_node.data self.index += 1 def print_ll(self): for i in range(len(self.lst)): print(self.lst[i], ' ', self.lst[i].nxt, end='\n') def __str__(self): str_1 = '' for items in self.lst: str_1 += str(items.data) + ' ' + str(items.nxt) + '\n' return str_1 def __len__(self): return len(self.lst) def __iter__(self): for items in self.lst: yield items def __repr__(self): str_1 = '' for items in self.lst: str_1 += str(items) str_1 += ' ' return str_1 Input: a = HashTable(1, [1, 1, 2, 3, 4, 5, 13]) print(a) Output: 1 1 2 3 4 5 13 1 1 2 3 4 5 13 1 1 2 3 4 5 13 1 1 2 3 4 5 13 1 1 2 3 4 5 13 1 1 2 3 4 5 13 1 1 2 3 4 5 13 1 1 2 3 4 5 13 1 1 2 3 4 5 13 1 1 2 3 4 5 13 1 1 2 3 4 5 13 1 1 2 3 4 5 13 1 1 2 3 4 5 13 1 1 2 3 4 5 13 1 1 2 3 4 5 13 1 1 2 3 4 5 13 1 1 2 3 4 5 13 1 1 2 3 4 5 13 1 1 2 3 4 5 13 1 1 2 3 4 5 13 1 1 2 3 4 5 13