i`m solving a basic exercise about say what number is duplicated in a list.
def repeat(nums):
for i in range(len(nums)):
x = nums[i]
lista2 = nums
lista2.pop(i)
for j in range(len(lista2)):
y = lista2[j]
if x == y:
return x
lista = [4,6,3,5,8,7,5,11] #The duplicated num is 5
print("Inicial list ", lista)
print("The duplicated is: ", repeat(lista)) #The function work but
print("List before function ", lista) #Destroy the inicial list
This is the result:
Inicial list [4, 6, 3, 5, 8, 7, 5, 11]
The duplicated is: 5
List before function [6, 5, 7, 11]
I know that exist other ways to solve this exercise, but i wanna know why this function delete elements of the initial list if i just copy the initial list and delete elements of the copy, not the original. More, the function eliminate the odd index elements of the list, someone can explain me why and how can i fix this function?