Sobes.tech
Junior — Senior

Updating dictionary values on matching user keys

livecode

Task condition

It is necessary to fix the implementation of the HashList class so that two objects with the same set of numbers are considered equal and when added to a dictionary, one replaces the other.

from typing import Any

class HashList:
    def __init__(self, data: list[int]) -> None:
        self._data = data

    def __eq__(self, other: Any) -> bool:
        if isinstance(other, self.__class__):
            return self._data == other._data
        return False

    def __hash__(self) -> int:
        return hash(sum(self._data))

obj1 = HashList([1, 2, 3])
obj2 = HashList([1, 2, 3])

my_dict = {
    obj1: "first",
    obj2: "second",
}

print(my_dict)