Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

group consecutive numbers in list python

import more_itertools as mit


iterable = [2, 3, 4, 5, 12, 13, 14, 15, 16, 17, 20]
[list(group) for group in mit.consecutive_groups(iterable)]
# [[2, 3, 4, 5], [12, 13, 14, 15, 16, 17], [20]]
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #group #consecutive #numbers #list #python
ADD COMMENT
Topic
Name
3+2 =