Sometimes, while working with Python lists, we can have a problem in which we might need to group the list elements on basis of their respective consecutiveness. This kind of problem can occur while data handling. Let’s discuss certain way in which this task can be performed.
Method : Using
enumerate() + groupby() + generator function + lambda
This task can be performed using the combination of above functions. In this, we create a generator function, in which we pass the list whose index-element are accessed using
enumerate() and grouped by consecutive elements using
groupby() and lambda. Works with Python2 only
The original list is : [1, 2, 3, 6, 7, 8, 11, 12, 13] Grouped list is : [(1, 3), (6, 8), (11, 13)]
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
- Python | Identical Consecutive Grouping in list
- Python - Convert List to key-value list by prefix grouping
- Python | Frequency grouping of list elements
- Python - List Elements Grouping in Matrix
- Python | Binary element list grouping
- Python | Grouping similar substrings in list
- Python | Grouping list values into dictionary
- Python - Value nested grouping on List
- Python - Summation Grouping in Dictionary List
- Python - Paired elements grouping
- Python | Grouping dictionary keys by value
- Python | K Division Grouping
- Python | Identical Strings Grouping
- Python - Case Insensitive Strings Grouping
- Python - Frequency Grouping Dictionary
- Python - Vertical Grouping Value Lists
- Python - Cross tuple summation grouping
- Python - Inter Matrix Grouping
- Grouping Rows in pandas
- Grouping Categorical Variables in Pandas Dataframe
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.