Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python - Fast Way to Remove Duplicates in This List?

Tags:

python

list

You know, to turn list:

a = ["hello", "hello", "hi", "hi", "hey"]

into list:

b = ["hello", "hi", "hey"]

You simply do it like this:

b = list(set(a))

It's fast and pythonic.

But what if i need to turn this list:

a = [["hello", "hi"], ["hello", "hi"], ["how", "what"], ["hello", "hi"], 
     ["how", "what"]] 

to:

b = [["hello", "hi"], ["how", "what"]]

What's the pythonic way to do it?

like image 293
Shane Avatar asked Jun 29 '12 12:06

Shane


1 Answers

>>> a = [["hello", "hi"], ["hello", "hi"], ["how", "what"], ["hello", "hi"], ["how", "what"]]
>>> set(map(tuple, a))
set([('how', 'what'), ('hello', 'hi')])
like image 177
jamylak Avatar answered Oct 16 '22 13:10

jamylak