1

The challenge is to find the first non-consecutive number in python. I have managed to do it with a library called more_itertools, but the challenge requires that no library be involved. How can I solve it? This is my code:

from more_itertools import consecutive_groups

def first_non_consecutive(l):
    g = []
    for grp in consecutive_groups(l):
        g.append(list(grp))
    if len(g) > 1:
        return g[1][0]
Ch3steR
  • 20,090
  • 4
  • 28
  • 58
Diesan Romero
  • 1,292
  • 4
  • 20
  • 45

1 Answers1

7

Use enumerate with start parameter to your advantage here.

def first_non_consecutive(lst):
    for i, j in enumerate(lst, lst[0]):
        if i!=j:
            return j

first_consecutive([1,2,3,4,6,7,8])
# 6
Ch3steR
  • 20,090
  • 4
  • 28
  • 58
  • I know this is old, but you can also do this ``` def nonconsecutive(arr: list) -> list: return [i for i, j in zip(arr, arr[1:]) if i+1 != j] ``` and then return the first value if you want all of them... – SumNeuron Aug 25 '23 at 15:04