I wrote this code which is a generator function that loops over the input iterable sequence, yielding one element at a time, but skipping duplicates. It then prints them.
seen = set()
for n in iterable:
if n not in seen:
seen.add(n)
yield n
numbers = [4, 5, 2, 6, 2, 3, 5, 8]
nums = unique(numbers)
print(next(nums))
print(next(nums))
print(next(nums))
print(next(nums))
print(next(nums))
print(next(nums))
print(next(nums))
things = unique(['dog', 'cat', 'bird', 'cat', 'fish'])
print(next(things))
print(next(things))
print(next(things))
print(next(things))
Right after the print(next(nums)), there is a stopiteration which I am ok with and is expected. My question is how to get past this to move on to the animals list.
My second question is whether there is any way to write this code without creating a list and returning the values.