So I am running into some trouble trying to debug this piece of code. I have a list of numbers, say [4,5,7,3,5,2,3] and I am required to find the two points who are closest together, so in this case, 3 and 3 since their difference is zero. However, it is not returning the correct output. It works if a number is not repeated in a list, but won't work if the a number appears more than once.
def closest1(num_list):
if len(num_list) < 2:
return (None, None)
else:
diff = max(num_list), min(num_list)
for element in num_list:
for sec_element in num_list:
if sec_element == element:
continue
if abs(sec_element - element) < abs(diff[0] - diff[1]):
diff = sec_element, element
return diff