Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 226
user541686 Avatar asked Dec 08 '22 19:12

user541686


1 Answers

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.

like image 158
user4815162342 Avatar answered Dec 11 '22 08:12

user4815162342