Python: Find First Non-Consecutive Integer in List

Using more_itertools.consecutive_groups

nick3499
1 min readJun 12, 2018
captured from Python3 REPL

To find the first non-consecutive integer in the list, I can use more_itertools.consecutive_groups to separate groups into map objects, then wrap them each in list() in order to print() each group as the for loop iterates through them.

>>> from more_itertools import consecutive_groups
>>> def first_non_consecutive(l):
... for grp in consecutive_groups(l):
... print(list(grp))
...
>>> print(first_non_consecutive([1,2,3,4,6,7,8]))
[1, 2, 3, 4]
[6, 7, 8]

The map objects can be printed.

>>> from more_itertools import consecutive_groups
>>> def first_non_consecutive(l):
... for grp in consecutive_groups(l):
... print(grp)
...
>>> print(first_non_consecutive([1,2,3,4,6,7,8]))
<map object at 0x7fcdb4250780>
<map object at 0x7fcdb42554e0>

--

--

nick3499
nick3499

Written by nick3499

coder of JavaScript and Python

No responses yet