Like this:
>>> seen = set() >>> words = ['the', 'counter', 'starts', 'the', 'starts', 'for'] >>> for x, w in enumerate(words, 1): ... seen.add(w) ... print(x, len(seen)) ... (1, 1) (2, 2) (3, 3) (4, 3) (5, 3) (6, 4)
In practice, I would make a generator function to consistently output tuples, rather than print them:
def uniq_count(lst): seen = set() for w in lst: seen.add(w) yield len(seen) counts = list(enumerate(uniq_count(words), 1))
Please note that I also separated the logic of the two counters. Since enumerate does exactly what you need for the first number in each pair, it is easiest to process the second number in the generator and enumerate process the first.
source share