def insert(key, value, D, hasher = hash):
x = hash(key)
key_value = (x, key, value)
D.append(key_value)
sorted(D, key = lambda x:x[0])
return D
insert("swag", 100, D, hash)
insert("swg", 150, D, hash)
print(D)
this never seems to be sorted,
I want to sort the list via the x value
You are sorting the list and immediately throwing the sorted list away. Note that
sorted
returns a new list so you'll also need to reassign the sorted list to the nameD
.To sort in-place, use the sort method of the list:
D.sort