I have a python list where elements can repeat.
>>> a = [1,2,2,3,3,4,5,6]
I want to get the first n unique elements from
n
Here is a Pythonic approach using itertools.takewhile():
itertools.takewhile()
In [95]: from itertools import takewhile In [96]: seen = set() In [97]: set(takewhile(lambda x: seen.add(x) or len(seen) <= 4, a)) Out[97]: {1, 2, 3, 4}