I am trying to create Python code which sorts an input list and creates tuples with number pairs which differ by 2:
def diff2(input_list):
input_list.sort()
result = []
for i in input_list:
for i2 in input_list:
if i + 2 is i2:
result.append((i, i2))
break
elif i + 2 < i2:
break
print(result)
return result
My test input data:
[49, 51, 75, 77, 109, 111, 119, 121, 121, 123, 124, 126, 126, 128, 170, 172, 290, 292, 308, 310, 318, 320, 444, 446, 562, 564, 564, 566, 566, 568, 568, 570, 570, 572, 633, 635, 674, 676, 711, 713, 713, 715, 901, 903, 903,905, 963, 965, 988, 990])
The code appends the touples correctly until the pair (121, 123). After the append (and corresponding break), for some reason the outer for cycle doesn't iterate further, but it holds its value at 121 for another cycle, than continues like nothing happened.
Also after (170, 172) the code seems to continue, (when I tried it in debug it even went into the append line) but still didn't do any appends. So it basically just stops at 170,172 while I think it should continue and create other number pairs as well (290, 292), (308, 310) etc.
Are the two problems related? What am I doing wrong?
Thanks in advance for any feedback.