If the sublists are already sorted like you input, just compare the previous to the current element, if the prev +1 is not equal to the current ele add prev + 1 to the output list:
List = [[1, 2, 4, 5], [1, 2, 3, 4, 5, 7], [1, 4, 5, 6]]
out = []
for sub in List:
prev = sub[0]
for ele in sub[1:]:
if prev + 1 != ele:
out.append(prev +1)
break
prev = ele
print(out)
[3, 6, 2]
It will work for any sublists that are ordered not just lists starting with 1:
List = [[3, 4, 5,7], [10,13,14,15], [100,101,104,105]]
Output:
[6, 11, 102]
And without slicing:
out = []
for sub in List:
prev = sub[0]
i = 0
for ele in sub:
if i == 0:
i += 1
continue
if prev + 1 != ele:
out.append(prev +1)
break
prev = ele
print(out)
If you always want to find the first missing number starting at 1
and can have 0 or negative numbers, only check each ele and increase i
if the ele is > 0:
out = []
for sub in Lists:
i = 1
for ele in sub:
if ele > 0:
if ele != i:
out.append(i)
break
i += 1
print(out)
That means at worst you do a single pass over each sublist as opposed to O(n^2)
approach using in