Sequences to Bags with Dask in Python 2026

Parallel Programming With Dask Mar 18, 2026

Sequences to Bags with Dask in Python 2026 – Best Practices

Dask Bags are excellent for processing sequences of Python objects such as lists, tuples, or custom records. Converting a Python sequence (or generator) into a Dask Bag enables parallel and distributed processing with minimal memory overhead.

TL;DR — Key Methods

  • db.from_sequence() — Convert a Python list, tuple, or generator into a Dask Bag
  • db.from_delayed() — Convert delayed objects into a Bag
  • Use .map(), .filter(), and .pluck() for transformations
  • Convert to Dask DataFrame when structure emerges

1. Basic Sequence to Bag


import dask.bag as db

# From a Python list
data = [{"id": i, "value": i*10, "region": "EU" if i % 3 == 0 else "US"} for i in range(100_000)]

bag = db.from_sequence(data, npartitions=50)

print("Number of partitions:", bag.npartitions)

# Parallel transformations
result = (
    bag.filter(lambda x: x["value"] > 500)
       .groupby(lambda x: x["region"])
       .map(lambda group: {"region": group[0], "total": sum(item["value"] for item in group[1])})
       .compute()
)

print(result)

2. From Generator to Bag (Memory Efficient)


def data_generator():
    for i in range(500_000):
        yield {"id": i, "amount": i * 2.5, "category": "A" if i % 5 == 0 else "B"}

# Convert generator directly to Dask Bag
bag = db.from_sequence(data_generator(), npartitions=100)

# Perform parallel aggregation
summary = (
    bag.filter(lambda x: x["amount"] > 1000)
       .groupby(lambda x: x["category"])
       .map(lambda g: {"category": g[0], "total": sum(x["amount"] for x in g[1])})
       .compute()
)

print(summary)

3. Best Practices in 2026

  • Use db.from_sequence() for lists or generators
  • Choose appropriate npartitions based on data size and available cores
  • Filter early using .filter() to reduce data volume
  • Convert to Dask DataFrame once data has clear tabular structure
  • Monitor the Dask Dashboard to see how partitions are being processed

Conclusion

Converting sequences and generators into Dask Bags is a powerful pattern for memory-efficient parallel processing. In 2026, this approach is commonly used for log processing, JSON data, and any irregular or streaming data sources that don’t fit neatly into DataFrames.

Next steps:

  • Try converting one of your generator-based or list-based processing scripts to a Dask Bag
Share:
Last updated: March 2026
Built-in Functions in Python 2026 str() in Python 2026: String Conversion + Modern Formatting & Best Practices memoryview with JAX in Python 2026: Zero-Copy NumPy → JAX Array Interop + Efficient ML Examples memoryview with TensorFlow in Python 2026: Zero-Copy NumPy → Tensor Interop + GPU Pinning & ML Examples memoryview with TensorFlow in Python 2026: Zero-Copy NumPy → Tensor Interop + ML Examples memoryview with NumPy & PyTorch in Python 2026: Zero-Copy Views, Efficient Slicing & ML Interop Examples memoryview with NumPy in Python 2026: Zero-Copy Views, Efficient Slicing & Real ML Examples memoryview() in Python 2026: Zero-Copy Magic for Large Binary Data + Real Examples import() in Python 2026: How It Works, Security Risks & Modern Alternatives (importlib) zip() in Python 2026: How to Use It, strict=True Behavior & Real-World Examples vars() in Python 2026: Accessing Object Namespace + Modern Introspection Patterns type() in Python 2026: Dynamic Type Inspection & Object Creation + Modern Patterns tuple() in Python 2026: Immutable Sequences + Modern Patterns & Best Practices super() in Python 2026: Method Resolution & Modern Inheritance Patterns sum() in Python 2026: Summing Iterables + Modern Numeric Patterns & Best Practices staticmethod() in Python 2026: Static Methods, Utility Functions & Modern Best Practices sorted() in Python 2026: Sorting Iterables + Modern Patterns & Best Practices slice() in Python 2026: Creating Slice Objects + Modern Patterns & Best Practices setattr() in Python 2026: Dynamic Attribute Setting + Modern Patterns & Safety set() in Python 2026: Mutable Sets Creation + Modern Patterns & Best Practices