How to Explode an Array Field into Multiple Rows in Python

This code flattens a list of dictionaries by exploding each array field value into its own row, duplicating the other fields as needed.

Easy Python 3.9+ Aug 9, 2026 Data pipelines & processing 11 views 0 copies

Python code

21 lines
Python 3.9+
from collections import defaultdict

data = [
    {"id": 1, "name": "Alice", "tags": ["python", "data", "ai"]},
    {"id": 2, "name": "Bob", "tags": ["web", "devops"]},
    {"id": 3, "name": "Carol", "tags": []},
]

def explode_array_field(records, array_field):
    result = []
    for record in records:
        for value in record.get(array_field, []):
            exploded = {k: v for k, v in record.items() if k != array_field}
            exploded[array_field] = value
            result.append(exploded)
    return result

if __name__ == "__main__":
    exploded = explode_array_field(data, "tags")
    for row in exploded:
        print(row)

Output

stdout
{'id': 1, 'name': 'Alice', 'tags': 'python'}
{'id': 1, 'name': 'Alice', 'tags': 'data'}
{'id': 1, 'name': 'Alice', 'tags': 'ai'}
{'id': 2, 'name': 'Bob', 'tags': 'web'}
{'id': 2, 'name': 'Bob', 'tags': 'devops'}

How it works

The function iterates over each record and each element in the specified array field. For every element, it creates a shallow copy of the original dictionary excluding the array field, then adds the single element back under the same key. The result is a list of dictionaries where each array element becomes its own record, and records with empty arrays are omitted entirely. This pattern is equivalent to SQL's UNNEST or Spark's explode function.

Common mistakes

  • Modifying the original record dictionary instead of creating a new one.
  • Forgetting to handle records where the array field is missing or empty, which causes the row to disappear.
  • Using .copy() on the root dict but not realizing that nested values are still shared references.
  • Assuming the output preserves the original array values in place of the exploded single value.

Variations

  1. Use a list comprehension with nested loops: [dict(record, tags=tag) for record in records for tag in record.get('tags', [])].
  2. Use itertools.chain to flatten repeated elements.

Real-world use cases

  • Transforming JSON payloads with nested tags or categories into flat rows for a relational database.
  • Preparing feature vectors where each user has a list of skills and each skill becomes a separate training example.
  • Ingesting clickstream data with an array of events per session into a tabular format for analytics.

Sponsored

Run this sample

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

Open editor

More from Data pipelines & processing

Related tutorials and quizzes for this topic.