Python: Find First Non-Consecutive Integer in List
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>