Reputation: 210755
I need to store a set
of list
s hashed by identity: two lists are equal iff they are the same object.
Not only does using tuple
s not make much sense semantically, but I also need to mutate the lists sometimes (append a few elements to the end every once in a while), so I can't use a tuple
at all.
How do I store a hash set of lists hashed by identity in Python?
Upvotes: 4
Views: 2476
Reputation: 155416
Use dict
instead of set, and let the id
of the list be the key:
dct[id(lst)] = lst
Test for existence of list in the "set" using id(lst) in dct
.
Upvotes: 14