user541686
user541686

Reputation: 210755

How to store a hashtable of lists in Python (hashed by identity)?

I need to store a set of lists hashed by identity: two lists are equal iff they are the same object.

Not only does using tuples 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

Answers (1)

user4815162342
user4815162342

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

Related Questions