Python | Group consecutive list elements with tolerance

Sometimes, we might need to group list according to the consecutive elements in list. But a useful variation of this can also be a case in which we need to consider a tolerance level, i.e allowing a skip value between number and not being exactly consecutive but a “gap” is allowed between numbers. Let’s discuss an approach in which this task can be performed.

Method : Using generator
The brute method to perform this task. Using loop and generator, one can perform this task. The slicing is taken care by the yield operator and hence this problem is solved by a small check of tolerance as well.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of
# Group consecutive list elements with tolerance
# Using generator
  
# helper generator
def split_tol(test_list, tol):
    res = []
    last = test_list[0]
    for ele in test_list:
        if ele-last > tol:
            yield res
            res = []
        res.append(ele)
        last = ele
    yield res
  
# initializing list
test_list = [1, 2, 4, 5, 9, 11, 13, 24, 25, 26, 28]
  
# printing original list
print("The original list is : " + str(test_list))
  
# Group consecutive list elements with tolerance
# Using generator
res = list(split_tol(test_list, 2))
  
# printing result 
print("The splitted list is : " + str(res))

chevron_right


Output :

The original list is : [1, 2, 4, 5, 9, 11, 13, 24, 25, 26, 28]
The splitted list is : [[1, 2, 4, 5], [9, 11, 13], [24, 25, 26, 28]]


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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 :

Be the First to upvote.


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