Last active
March 2, 2024 11:57
-
-
Save ZSendokame/7f571dbe12dd75e9e0e72c9e989ec99a to your computer and use it in GitHub Desktop.
flattening of dictionaries.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def flatten(structure: dict, head = [], flattened = {}) -> list: | |
for key, value in structure.items(): | |
if isinstance(value, dict): | |
head.append(key) | |
flatten(value, [*head, key], flattened) | |
else: | |
flattened['__'.join(head)] = value | |
return flattened |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment