How to Shuffle Items by Group in Python

Randomly shuffle items within each group while keeping groups contiguous, using a seed for reproducible results.

Easy Python 3.9+ Aug 9, 2026 Big data & Spark 13 views 0 copies

Python code

33 lines
Python 3.9+
import random

def shuffle_sort_groups(items, group_key, seed=None):
    """Randomize order within groups, keeping groups contiguous."""
    rng = random.Random(seed)
    
    groups = {}
    for item in items:
        key = group_key(item)
        groups.setdefault(key, []).append(item)
    
    result = []
    for key in groups:
        group_items = groups[key]
        rng.shuffle(group_items)
        result.extend(sorted(group_items, key=group_key))
    
    rng.shuffle(result)
    return result


if __name__ == "__main__":
    data = [
        {"name": "apple", "category": "fruit"},
        {"name": "banana", "category": "fruit"},
        {"name": "carrot", "category": "veg"},
        {"name": "date", "category": "fruit"},
        {"name": "eggplant", "category": "veg"}
    ]
    
    shuffled = shuffle_sort_groups(data, lambda x: x["category"], seed=42)
    for item in shuffled:
        print(f"{item['name']} -> {item['category']}")

Output

stdout
banana -> fruit
apple -> fruit
date -> fruit
carrot -> veg
eggplant -> veg

How it works

This function first groups items by the group_key callback, preserving the original order within each group. It then shuffles each group's items with a seeded random.Random instance, so the same seed always produces the same permutation. After sorting each group by its key to keep items together, the function shuffles the final list of groups to randomize group order while keeping each group's elements contiguous. The seed parameter ensures reproducibility, which is useful in testing and data pipelines.

Common mistakes

  • Using `random.shuffle` directly instead of creating a `Random` instance, which breaks reproducibility.
  • Forgetting to sort or group before shuffling, which can separate items from the same group.
  • Assuming the order of groups is stable; the final shuffle intentionally randomizes it.

Variations

  1. Use `random.seed()` once before the function to affect all random calls globally.
  2. Use `groupby` from `itertools` after sorting the input if the data is already ordered by group.

Real-world use cases

  • Shuffling user IDs for A/B test bucketing while keeping cohorts together.
  • Randomizing test data within categories for balanced evaluation sets.
  • Shuffling log lines per service during Spark-based analysis for privacy-preserving samples.

Sponsored

Run this sample

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

Open editor

More from Big data & Spark

Related tutorials and quizzes for this topic.