In searching for a method to find the duplicate entries in a list in python someone posted a solution that works in python and I cant decipher the ternary logic.
The post was here How do I find the duplicates in a list and create another list with them?
The code is:
seen = set()
dupes = [x for x in list if x in seen or seen.add(x)]
The original poster helpful showed what it equates to:
dupes = []
for x in list:
if x in seen:
dupes.append(x)
else:
seen.add(x)
I cant find any reference googling around to a ternary of the form if x .... or ...
Can someone help me break down all the logic here?
I roughly understand how dupes = [x equates to dupes.append(x) when 'if x in seen' is True.
But I cant sort out how the 'or seen.add(x)' equates to calling else: seen.add(x) when x was not found in the seen list and thus adds nothing to the dupes list.