How to remove duplicate lists in a list of list?

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.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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) )

Method 2

See mgilson’s answer if the order of the lists is not important. If you want to retain the order, do something like:

b = list()
for sublist in a:
    if sublist not in b:
        b.append(sublist)

This will keep the order in the original list. However, it is slower and more verbose than using sets.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x