Python | Make a list of intervals with sequential numbers

Given a list of sequential numbers, Write a Python program to convert the given list into list of intervals.

Examples:

Input : [2, 3, 4, 5, 7, 8, 9, 11, 15, 16]
Output : [[2, 5], [7, 11], [15, 16]]

Input : [1, 2, 3, 6, 7, 8, 9, 10]
Output : [[1, 3], [6, 10]]

Method #1 : Naive Appraoch

First, we use the brute force approach to Convert list of sequential number into intervals. Start a loop till the length of the list. In every iteration, use another loop to check the continuity of the sequence. As soon as the sequence stop, yield the lower and higher bound of each interval.

 # Python3 program to Convert list of  # sequential number into intervals    def interval_extract(list):     length = len(list)     i = 0     while (i< length):         low = list[i]         while i = 1):             yield [low, high]         elif (high - low == 1):             yield [low, ]             yield [high, ]         else:             yield [low, ]         i += 1    # Driver code l =  [2, 3, 4, 5, 7, 8, 9, 11, 15, 16] print( list(interval_extract(l)))

Output:

[[2, 5], [7, 9], , [15, 16]]

Method #2 : Pythonic Naive
First, sort the given list. Initialize previous_number and range_start with first element. Start a loop and check if the next number is additive of the previous number, If yes, Initialize this number to previous number otherwise yield the new interval starting with range_start and ending with previous_number.

 # Python3 program to Convert list of  # sequential number into intervals    def interval_extract(list):     list = sorted(set(list))     range_start = previous_number = list        for number in list[1:]:         if number == previous_number + 1:             previous_number = number         else:             yield [range_start, previous_number]             range_start = previous_number = number     yield [range_start, previous_number]    # Driver code l = [2, 3, 4, 5, 7, 8, 9, 11, 15, 16] print( list(interval_extract(l)))

Output:

[[2, 5], [7, 9], [11, 11], [15, 16]]

Method #3 : Using itertools
The other pythonic method is to use Python itertools. We use itertools.groupby(). Where enumerate(iterable) is taken as iterable and lambda t: t – t) as key function to find the sequence for intervals.

 # Python3 program to Convert list of  # sequential number into intervals import itertools    def intervals_extract(iterable):            iterable = sorted(set(iterable))     for key, group in itertools.groupby(enumerate(iterable),     lambda t: t - t):         group = list(group)         yield [group, group[-1]]    # Driver code l = [2, 3, 4, 5, 7, 8, 9, 11, 15, 16] print( list(intervals_extract(l)))

Output:

[[2, 5], [7, 9], [11, 11], [15, 16]]

My Personal Notes arrow_drop_up If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.