This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Let's create a list with duplicated elements: | |
ls = [1,1,1,2,2,1,5,4,3,2,1] | |
# And let's now remove them in just one line: | |
l2 = list(set(ls)) | |
# See the result: | |
print l2 | |
[1, 2, 3, 4, 5] |
Voilà!
UPDATE:
This way the order is preserved and looks nice:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from collections import OrderedDict | |
list(OrderedDict.fromkeys(xs)) |
+Franco Pellegrini
2 comentarios:
Guarda con eso porque no te preserva el orden...
Algo (muy feo) que podrias hacer es
>>> [i for i in l if i not in locals()['_[1]']]
En mi caso no importaba el orden, sólo que se eliminaran los duplicados.
Buen comentario de todos modos, Frapell.
Publicar un comentario