Flatten It

Introduction

Flatten It uses iteration (no recursion) to flatten iterables into a dictionary where each key is a reference path and each value is the value at the respective path.  This simple flattening algorithm has been used for diffing deeply nested JSON structures and for transforming hierarchical data into a tabular data model.

 

Features

  • Produces valid Python or JS reference paths for string and numeric keys.
  • Raises ValueError on circular references.
  • Iterative algorithm; flatten deeply nested structures without causing a call stack overflow.

 

Code

 

Resources

About the Author

Adam Patterson has decades of experience working in software engineering and data engineering roles and he holds a Master’s of Applied Data Science from the University of Michigan.  Adam has worked in IT domains including Education, Health Sciences, and Facilities and Asset Management.  He currently serves as a Data Engineer and Trusted IT Service Provider for the Neurology Department at Michigan Medicine.

 |  |  | 

 

Print Article

Related Articles (2)

Memoiz provides a function decorator that adds memoization to a function or method. It makes reasonable assumptions about how and if to cache the return value of a function or method based on the arguments passed to the callable.
Overview of the Sleep Data Automation code, a project developed by the Mobile Technologies Core and the Sleep & Circadian Rhythm Laboratory to automate the process of cleaning sleep data from Fitbit fitness trackers and comparing or joining it to self-reported sleep diary data.