I am trying to find the missing numbers in an arbitrary list. In an ordered list my code works fine, but in an arbitrary list in doesn't work. This is my code:
a = [10,12,13,8]
b = [x for x in range(a[0], a[-1] + 1)]
a = set(a)
print(list(a ^ set(b)))
The output is:
[8, 10, 12, 13]
But when a
is sorted, the output comes out fine:
[9,11]
What's wrong in my code and how can i fix it? PS. I know i can just sort the list and get the problem fixed but i want it to work on an arbitary list as well.