Why this code eliminate all duplicate!? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 4

Why this code eliminate all duplicate!?

lista=[1,2,3,4,4,3,6,7,8,3,8,7,9,"hola",True,2.2,"hola",2.2,True] print (lista) lista = (set(lista)) for x in lista: print (x) print (lista)

3rd Feb 2017, 2:53 AM
Christian Navarro
Christian Navarro - avatar
5 Answers
+ 3
set() te permite crear un arreglo no indexado llamado CONJUNTO. Al hacer set(lista) estas creando un CONJUNTO con los datos de la lista y en los conjuntos no hay elementos repetidos.Si luego haces list(set(lista)), conviertes el conjunto en una lista nuevamente, donde la nueva lista ya no tiene los elementos repetidos. set () allows you to create an unindexed array called SET. When doing set(lista) you are creating a SET with the data of the list and in the sets there are no repeated elements. If you do list(set(lista)), you convert the set into a list again, where the new list no longer Has the elements repeated. PD: Aunque no haces list(set()), haces (set()) con lo que estas convirtiendo a tu conjunto realmente en una tupla y no en una lista. PS: Although you do not list(set()), you do (set()) with what you are actually converting to your set in a tuple and not in a list.
3rd Feb 2017, 3:10 AM
Javier I. Rivera R.
Javier I. Rivera R. - avatar
+ 2
You turned lista into a set, which automatically gets rid of duplicates by default.
3rd Feb 2017, 3:00 AM
DaemonThread
DaemonThread - avatar
+ 1
i only switch my lista for set , only for that ? ... thankz
3rd Feb 2017, 3:03 AM
Christian Navarro
Christian Navarro - avatar
+ 1
entonces sólo debería poner en el código list(set(lista)) y seguiría siendo una lista indexada!?
3rd Feb 2017, 3:17 AM
Christian Navarro
Christian Navarro - avatar
0
Eso es correcto / It's right
3rd Feb 2017, 3:24 AM
Javier I. Rivera R.
Javier I. Rivera R. - avatar