Getting first n unique elements from Python list

后端 未结 12 1094
無奈伤痛
無奈伤痛 2021-02-04 23:59

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

12条回答
  •  无人及你
    2021-02-05 00:45

    Why not use something like this?

    >>> a = [1, 2, 2, 3, 3, 4, 5, 6]
    >>> list(set(a))[:5]
    [1, 2, 3, 4, 5]
    

提交回复
热议问题