Alternatively to the option given by the partner javdr you can use loops for
nested, where the first for
go through elements of the list, and in the second for
that goes comparing them with the elements of the first for
, to understand it better, I exemplify:
numeros = [1,-1,1,3,9,5]
def comparar(lista):
indice_c = -1 # Se asigna a 0 para que no de error al comparar el index en el for
for elemento in lista:
indice_c = -1
indice = lista.index(elemento) # Se asigna el numero de indice del elemento a comparar
for comparacion in lista: # Se toma el elemento a comparar
indice_c += 1
if indice_c == indice: # Esto es para evitar que se confunda el programa y compare un
# elemento consigo mismo, lo cual no tendria sentido
continue # Se reinicia el bucle
elif elemento == comparacion: # Si el elemento de la lista es igual al elemento
# comparado, se retorna un False
return False
return True # De no haber elementos iguales, se retorna un True
print (comparar(numeros))
Output with repeated element
# numeros = [1,-1,1,3,9,5]
False
Output, now without repeated element:
# numeros = [1,-1,3,9,5]
True
If you do not understand, you can use Visual Studio Code to debug the code and view the variables in real time.
Clearly, the option that gave you javdr is much more simple and elegant , but I give you
this alternative anyway.
Regards