-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathflatten_dictionary.py
63 lines (51 loc) · 1.73 KB
/
flatten_dictionary.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
"""
Flatten a Dictionary
Given a dictionary dict, write a function flattenDictionary that returns a flattened version of it.
If a certain key is empty, it should be excluded from the output (see e in the example below).
input: dict = {
"Key1" : "1",
"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"
}
"""
# O(n) time, where n is the number of keys in the input dictionary
# O(n) space
def flatten_dictionary(nested_dict):
flat_dict = {}
flat_dict_recurse(nested_dict, flat_dict)
return flat_dict
def flat_dict_recurse(nested_dict, flat_dict, path=""):
for key, value in nested_dict.items():
include_dot = 1 if path and key else 0
updated_path = path + "." * include_dot + key
if not isinstance(value, dict):
flat_dict[updated_path] = value
else:
flat_dict_recurse(value, flat_dict, updated_path)
# Another way of solving the problem
def flatten_dictionary(nested_dict):
if not isinstance(nested_dict, dict):
return {"": nested_dict}
flat_dict = {}
for key, value in nested_dict.items():
flat_items = flatten_dictionary(value)
for key_of_flat_item, value_of_flat_item in flat_items.items():
add_dot = 1 if key and key_of_flat_item else 0
concat_key_of_flat_item = key + ("." * add_dot) + key_of_flat_item
flat_dict[concat_key_of_flat_item] = value_of_flat_item
return flat_dict