Python - List comprehension break -


i have following list:

a = [[1,'abc',0],[1,'abc',2],[1,'abc',3],[2,'kak',0],[2,'kak',1],[2,'kak',2],[3,'kok,0]] 

i want create list comprehension line returns me b list:

b = [[1,'abc',0],[2,'kak',0],[3,'kok',0]] 

the list created unique values of first column of entries of a.

i tried this:

aux1 = list(set([c[0] c in a])) b    = [[c c in break if c[0] == i] in aux1] 

this code solves problem, not list comprehension:

a = [[1,'abc',0],[1,'abc',2],[1,'abc',3],[2,'kak',0],[2,'kak',1],[2,'kak',2],[3,'kok',0]]  b = []  aa in a:     new = true     bb in b:                 if bb[0]==aa[0]:             new=false             break     if new:         b.append(aa) 

if not care second or third element in inner list taken list b , can use itertools.groupby -

>>> b = [next(i) x,i in groupby(sorted(a, key = lambda x: x[0]),lambda x: x[0])] >>> b [[1, 'abc', 0], [2, 'kak', 0], [3, 'kok', 0]] 

Comments