Skip to content

Instantly share code, notes, and snippets.

@scuerda
Created July 27, 2016 14:31
Show Gist options
  • Save scuerda/ded304c86653788a2a942d749c6b657b to your computer and use it in GitHub Desktop.
Save scuerda/ded304c86653788a2a942d749c6b657b to your computer and use it in GitHub Desktop.
# Simple python function to flatten a dict with a regular structure into a list of dicts
# This is essentially the inverse of a d3 roll-up, where we know that the deepest leaves will contain all necessary keys
def rollout(d, current_level=0, depth_to_flatten=0):
items = []
for k, v in d.items():
if current_level == depth_to_flatten:
if v == {}:
pass
else:
items.append(v)
else:
l = current_level + 1
items.extend(flatten_to_depth(v, l, depth_to_flatten))
return items
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment