Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove duplicate lists in a list of list? [duplicate]

Tags:

python

I want to remove all duplicates list from a list of list.

So I have a list of lists like this.

a = [[1,2],[1,2],[3,4,5],[3,4,5],[3,4,5]] 

I want to have:

b = [[1,2],[3,4,5]] 

I don't know how to do it.

like image 566
user1507156 Avatar asked Aug 30 '12 13:08

user1507156


1 Answers

You could use a set:

b_set = set(map(tuple,a))  #need to convert the inner lists to tuples so they are hashable b = map(list,b_set) #Now convert tuples back into lists (maybe unnecessary?) 

Or, if you prefer list comprehensions/generators:

b_set = set(tuple(x) for x in a) b = [ list(x) for x in b_set ] 

Finally, if order is important, you can always sort b:

b.sort(key = lambda x: a.index(x) ) 
like image 149
mgilson Avatar answered Sep 21 '22 14:09

mgilson