Search code examples
pythongeneratoryield

How to not return duplicate values from a generator?


I have a function like below:

def fun(content):
    for i in content:
        id = i.split('\"')[0]
        yield id    
    return id

The problem is that there are some duplicated values in content. Is there any way to know if the value 'id' is already in the generator 'id'? Rather than get the final generator then use set()?


Solution

  • You can use a set inside fun to keep track of the ids that have been seen already:

    def fun(content):
        observed = set()
        for i in content:
            id = i.split('\"')[0]
            if id not in observed:
                observed.add(id)
                yield id
    

    Also, since you're yielding ids you don't need to return at the end.