Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

Python | Consecutive elements grouping in list - GeeksforGeeks https://www.geeksforgeeks.org/python-consecutive-element...

DSA Data Structures Algorithms Interview Preparation Topic-wise Practice C++ Java

Python | Consecutive elements grouping


in list
Last Updated : 09 Sep, 2019

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

# Python code to demonstrate working of


# Consecutive elements grouping list
# using enumerate() + groupby() + generator function + lambda
import itertools
  
# Utility Generator Function
def groupc(test_list):
    for x, y in itertools.groupby(enumerate(test_list), lambda (a, b): b
        y = list(y)
        yield y[0][1], y[-1][1]
  
# initialize list
test_list = [1, 2, 3, 6, 7, 8, 11,
▲ 12, 13]

1 of 5 11/4/22, 21:01
Python | Consecutive elements grouping in list - GeeksforGeeks https://www.geeksforgeeks.org/python-consecutive-element...

  
# printing original list
print("The original list is : " + str(test_list))
  
# Consecutive elements grouping list
# using enumerate() + groupby() + generator function + lambda
res = list(groupc(test_list))
  
# printing result
print("Grouped list is : " + str(res))

Output :

The original list is : [1, 2, 3, 6, 7, 8, 11, 12, 13]


Grouped list is : [(1, 3), (6, 8), (11, 13)]

Like 0

Next

Python | Consecutive
elements pairing in list

R ECO M M E N D E D A RT I C L E S Page : 1 2 3

Python | Identical Consecutive Python | Binary element list


01 Grouping in list 05 grouping
02, Sep 19 02, Jan 19

Python - Convert List to key- Python | Grouping similar


02 value list by prefix grouping 06 substrings in list
30, Jul 20 11, Mar 19

2 of 5 11/4/22, 21:01
Python | Consecutive elements grouping in list - GeeksforGeeks https://www.geeksforgeeks.org/python-consecutive-element...

Python | Frequency grouping of Python - Value nested grouping


03 list elements 07 on List
18, Nov 19 21, Apr 20

Python - List Elements Python - Summation Grouping


04 Grouping in Matrix 08 in Dictionary List
29, Aug 20 30, Apr 20

Article Contributed By :

manjeet_04
@manjeet_04

Vote for di�culty

Easy Normal Medium Hard Expert

Article Tags : Python list-programs, Python

Practice Tags : python

Improve Article Report Issue

Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.

3 of 5 11/4/22, 21:01
Python | Consecutive elements grouping in list - GeeksforGeeks https://www.geeksforgeeks.org/python-consecutive-element...

Load Comments

A-143, 9th Floor, Sovereign Corporate Tower,


Sector-136, Noida, Uttar Pradesh - 201305

feedback@geeksforgeeks.org

Company Learn

About Us Algorithms

Careers Data Structures

In Media SDE Cheat Sheet

Contact Us Machine learning

Privacy Policy CS Subjects

Copyright Policy Video Tutorials

Courses

News Languages
Top News
Python
Technology
Java
Work & Career
CPP
Business
Golang
Finance
C#
Lifestyle
SQL
Knowledge
Kotlin

Web Development Contribute

Web Tutorials Write an Article

Django Tutorial Improve an Article

HTML Pick Topics to Write

JavaScript ▲ Write Interview Experience

4 of 5 11/4/22, 21:01
Python | Consecutive elements grouping in list - GeeksforGeeks https://www.geeksforgeeks.org/python-consecutive-element...

Bootstrap Internships

ReactJS Video Internship

NodeJS

@geeksforgeeks , Some rights reserved

5 of 5 11/4/22, 21:01

You might also like