Skip to content

Instantly share code, notes, and snippets.

@HauptJ
Last active February 27, 2020 13:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save HauptJ/dc274569bb493c4a2e62e139bbf477da to your computer and use it in GitHub Desktop.
Save HauptJ/dc274569bb493c4a2e62e139bbf477da to your computer and use it in GitHub Desktop.
def flatten_dictionary(dictionary):
flattened = {}
flattened = flatten_helper("", dictionary, flattened)
return flattened
def flatten_helper(initKey, dictionary, flattened):
for k, v in dictionary:
if not isInstance(v, dict):
if initKey is None or initKey == "":
flattened[k] = v
return flattened
else:
flattened[initKey + "." + k] = v
return flattened
else:
if initKey is None or initKey == "":
flatten_helper(k, v, flattened)
else:
flatten_helper(initKey + "." + k, v, flattened)
"""
cant hear u can u refresh
time:
meaning??
key1 1 <-
nested(dictlistkey):
if len(keys[values]) > 1
def flatten(dictList, KVList, index=1):
if index = len(dictList):
return KVList
if nested(dictListkey) is False:
newKV = (key: value)
KVList.append(newKV)
else:
dictListindex = dictListindex+1[key] + dictList+1[value]
flatten(dictList, KVList, prev_key + "." + current_key)
return
{
"Key1" : "1", < - base
"Key2" : { <- recursive
"a" : "2"
}
}
key2
a: 2
b: 3
c:
d: 3
e:
"" : 1
output: {
"Key1" : "1", <-
"Key2.a" : "2",
"Key2.b" : "3",
"Key2.c.d" : "3",
"Key2.c.e" : "1"
}
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment