Skip to content

Instantly share code, notes, and snippets.

View boabdilperez's full-sized avatar

Boabdil Perez boabdilperez

  • Texas
  • 06:17 (UTC -05:00)
View GitHub Profile
@boabdilperez
boabdilperez / flatten_dict.py
Last active October 19, 2023 18:52
Turns a dictionary into a list of lists
def flatten_dict(input_dict, antecedent=None):
"""Returns a generator that yields every possible branch of data"""
antecedent = antecedent[:] if antecedent else []
if isinstance(input_dict, dict):
for key, value in input_dict.items():
if isinstance(value, dict):
for nested_dict in flatten_dict(
value, antecedent + [key]
):
yield nested_dict