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.
Python code
21 linesfrom 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
{'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
- Use a list comprehension with nested loops: [dict(record, tags=tag) for record in records for tag in record.get('tags', [])].
- 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
More from Data pipelines & processing
- Add a UUID Surrogate Key to Each Row in a CSV with Python easy
- Attach Source File Metadata to Records in Python easy
- Build a Python Utility That Detects Duplicate Records Across Multiple Excel Sheets medium
- Check Null Rate Threshold in PySpark DataFrame medium
- Count Records Processed per Category in Python easy
- Create Data Helper Functions in Python for Beginners easy
Keep learning
Related tutorials and quizzes for this topic.