I want to check if an item is already in a list with referential equality and not with strucural equality.
For clarity:
referential equality between 2 items is checked with
item1 is item2
structural equality is checked with
item1 == item2
Structural equality for checking if an item is already in a list is easily done like this:
item in list
So I'm looking for equivalent line with referential equality. Is this possible without looping over every item in the list?
An example of how this needs to be implemented (just for clarification):
def get_all_items_in_list(items):
all_items = []
for item in items:
if not item in all_items: # replace with referential equality here
all_items.append(item)
return all_items
# setup
a = (1, [])
b = (1, [])
print(a is b) # prints False
items = [a, a, b]
print(get_all_items_in_list(items)) # should print [(1, []), (1, [])] but prints [(1, [])]