Group Consecutive Keys in Python with itertools.groupby

Group consecutive equal elements in a list using the itertools.groupby generator, printing each key and its values.

Easy Python 3.9+ Aug 9, 2026 Comprehensions & generators 11 views 0 copies

Python code

7 lines
Python 3.9+
from itertools import groupby

data = [1, 1, 2, 2, 3, 1, 1, 4, 4, 4]

for key, group in groupby(data):
    group_list = list(group)
    print(f"Key: {key}, Values: {group_list}")

Output

stdout
Key: 1, Values: [1, 1]
Key: 2, Values: [2, 2]
Key: 3, Values: [3]
Key: 1, Values: [1, 1]
Key: 4, Values: [4, 4, 4]

How it works

itertools.groupby returns consecutive keys and group iterators for adjacent equal elements. The groups are lazy iterators, so you must consume them (e.g., with list()) before the next iteration of the outer loop. The key function defaults to identity — the element itself — so equal adjacent values form groups. groupby only groups consecutive runs, not all equal elements across the sequence, which is why the repeated 1s appear as separate groups when non-adjacent. This behavior is ideal for run-length encoding or tokenizing consecutive patterns.

Common mistakes

  • Assuming `groupby` groups all equal elements — it only groups consecutive runs
  • Forgetting to convert the group iterator to a list before the next loop iteration since it is consumed lazily
  • Using a non-deterministic key function that varies between calls

Variations

  1. Pass a `key` argument to group by a transformed value, e.g., `groupby(words, key=str.lower)`
  2. Use a list comprehension `[list(g) for _, g in groupby(data)]` to collect all groups at once

Real-world use cases

  • Run-length encoding for image compression or data compaction where consecutive duplicate pixels are stored as (value, count) pairs.
  • Parsing log files to group consecutive error lines into single alert blocks for deduplicated monitoring notifications.
  • Segmenting a timeseries into stable-state intervals so a dashboard only redraws when the metric actually changes.

Sponsored

Run this sample

Open the browser IDE to tweak the example and see results without installing anything.

Open editor

More from Comprehensions & generators

Related tutorials and quizzes for this topic.