How to remove duplicate items from a list using list comprehension?

前端 未结 7 1107
庸人自扰
庸人自扰 2020-11-30 11:43

How to remove duplicate items from a list using list comprehension? I have following code:

a = [1, 2, 3, 3, 5, 9, 6, 2, 8, 5, 2, 3, 5, 7, 3, 5, 8]
b = []
b =         


        
7条回答
  •  伪装坚强ぢ
    2020-11-30 12:00

    If you don't mind using a different technique than list comprehension you can use a set for that:

    >>> a = [1, 2, 3, 3, 5, 9, 6, 2, 8, 5, 2, 3, 5, 7, 3, 5, 8]
    >>> b = list(set(a))
    >>> print b
    [1, 2, 3, 5, 6, 7, 8, 9]
    

提交回复
热议问题