Python | Group elements on break positions in list
Many times we have problems involving and revolving around Python grouping. Sometimes, we might have a specific problem in which we require to split and group N element list on missing elements. Let’s discuss a way in which this task can be performed. Method : Using itemgetter() + map() + lambda() + groupby() This task can be performed using the combination of above functions in which we can group the elements in breaks calculated using lambda function by finding difference between the index and value in list. The map() is used to combine the logic and itemgetter ensures the grouping is on value. Works with Python2 only.
(x, y) : x
("Grouping of elements at breaks : "
The original list is : [1, 2, 4, 5, 6, 8, 9, 11]
Grouping of elements at breaks : [[1, 2], [4, 5, 6], [8, 9], ]
Time Complexity: O(n)
Auxiliary Space: O(n)
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses
are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!