How to reduce lines with list comprehensions in Python? -


i have syntax :

l_one = [ x x in somelist ] # list duplicates l = [] l_two = [ l.append( y ) y in l_one if y not in l ] # duplicates removed 

both l , l_two same lists without duplicates. there way reduce lines , maybe have oneliner?

edit : correction - l_two "noned" list.

actually, aren't same. .append() returns none because modifies list in place, l_two list bunch of nones. however, l list no dupes.

if want remove duplicates list, can make set:

l_two = list(set(l_one)) 

note remove order.


try using for-loop instead of list comprehension if want use unhashable types:

l_one = [x x in somelist] l_two = [] in l_one:     if not in l_two:         l_two.append(i) 

or:

from itertools import groupby l_two = [key key, value in groupby(l_one)] 

Comments