I've build the following code:
def pairs(num_list, n):
list1 = []
for num1 in num_list:
for num2 in num_list:
if num1 + num2 == n:
list1.append([num1, num2])
return(list1)
print(pairs([2,3,4,5],7))
The output is:
[[2, 5], [3, 4], [4, 3], [5, 2]]
BUT I only need NON REPETITIVE PAIRS TO SHOW for ex. [[2, 5], [3, 4]] (doesn't matter which pair)
I figured that I need to go through the numbers more efficiently like so: the first loop goes through 2. second loop compares it to 3,4,5 (without 2) then the first loop goes through 3. second loop goes through 4,5 (without 2 or 3) and so on. How can I actually do this with a code?