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.
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]]
- Python - Group first elements by second elements in Tuple list
- Python | Group elements at same indices in a multi-list
- Python | Group list elements based on frequency
- Python | Group elements on break positions in list
- Python | Binary Group Tuple list elements
- Python - Group elements from Dual List Matrix
- Python - Group Sublists by another List
- Python | Group by matching second tuple value in list of tuples
- Python | Group tuples in list with same first value
- Python | Group Anagrams from given list
- Python | Group strings at particular element in list
- Python | Group tuple into list based on value
- Python - Group list by first character of string
- Python | Group List on K character
- Python - Group similar value list to dictionary
- Python - Group records by Kth column in List
- Python - Group keys to values list
- Python - Group single item dictionaries into List values
- Group List of Dictionary Data by Particular Key in Python
- Python - Group Similar items to Dictionary Values List
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.