How to flatten nested python dictionaries?
Question:
I’m trying to flatten the nested dictionary:
dict1 = {
'Bob': {
'shepherd': [4, 6, 3],
'collie': [23, 3, 45],
'poodle': [2, 0, 6],
},
'Sarah': {
'shepherd': [1, 2, 3],
'collie': [3, 31, 4],
'poodle': [21, 5, 6],
},
'Ann': {
'shepherd': [4, 6, 3],
'collie': [23, 3, 45],
'poodle': [2, 10, 8],
}
}
I’d like to display all values in lists:
[4, 6, 3, 23, 3, 45, 2, 0, 6, 1, 2, 3,…, 2, 10, 8]
My first idea was to do in this way:
dict_flatted = [ i for name in names.values() for dog in dogs.values() for i in dog]
Though I’m getting the error. I’d be happy for tips how to handle it.
Answers:
You can use a simple recursive function as follows.
def flatten(d):
res = [] # Result list
if isinstance(d, dict):
for key, val in d.items():
res.extend(flatten(val))
elif isinstance(d, list):
res = d
else:
raise TypeError("Undefined type for flatten: %s"%type(d))
return res
dict1 = {
'Bob': {
'shepherd': [4, 6, 3],
'collie': [23, 3, 45],
'poodle': [2, 0, 6],
},
'Sarah': {
'shepherd': [1, 2, 3],
'collie': [3, 31, 4],
'poodle': [21, 5, 6],
},
'Ann': {
'shepherd': [4, 6, 3],
'collie': [23, 3, 45],
'poodle': [2, 10, 8],
}
}
print( flatten(dict1) )
You’re trying to use variables that don’t exist.
Use this
dict_flatted = [ i for names in dict1.values() for dog in names.values() for i in dog]
The recursive function can be single line:
def flatten(d):
return d if isinstance(d, list) else sum((flatten(i) for i in d.values()), [])
Nota: was an answer for a duplicate question
I started with @DaewonLee’s code and extended it to more data types and to recurse within lists:
def flatten(d):
res = [] # type:list # Result list
if isinstance(d, dict):
for key, val in d.items():
res.extend(flatten(val))
elif isinstance(d, list):
for val in d:
res.extend(flatten(val))
elif isinstance(d, (float, str, int)):
res = [d] # type: List[float] or List[str] or List[int]
else:
raise TypeError("Undefined type for flatten: %s" % type(d))
return res
for this specific case: (2 levels)
-
using itertools.chain method:
nested_dict = {1 : {‘a’ : [1,2,3], ‘b’ : [4,5]},
2 : {‘a’ : [6], ‘b’ : [7,8,9]}}
list(chain(*[v for y in nested_dict.values() for v in list(y.values())]))
[1, 2, 3, 4, 5, 6, 7, 8, 9]
using pandas:
df = pd.DataFrame(pd.json_normalize(data=nested_dict), dtype=object)
df['chain'] = df.apply(lambda x: list(chain(*x.values.tolist())), axis=1)
I’m trying to flatten the nested dictionary:
dict1 = {
'Bob': {
'shepherd': [4, 6, 3],
'collie': [23, 3, 45],
'poodle': [2, 0, 6],
},
'Sarah': {
'shepherd': [1, 2, 3],
'collie': [3, 31, 4],
'poodle': [21, 5, 6],
},
'Ann': {
'shepherd': [4, 6, 3],
'collie': [23, 3, 45],
'poodle': [2, 10, 8],
}
}
I’d like to display all values in lists:
[4, 6, 3, 23, 3, 45, 2, 0, 6, 1, 2, 3,…, 2, 10, 8]
My first idea was to do in this way:
dict_flatted = [ i for name in names.values() for dog in dogs.values() for i in dog]
Though I’m getting the error. I’d be happy for tips how to handle it.
You can use a simple recursive function as follows.
def flatten(d):
res = [] # Result list
if isinstance(d, dict):
for key, val in d.items():
res.extend(flatten(val))
elif isinstance(d, list):
res = d
else:
raise TypeError("Undefined type for flatten: %s"%type(d))
return res
dict1 = {
'Bob': {
'shepherd': [4, 6, 3],
'collie': [23, 3, 45],
'poodle': [2, 0, 6],
},
'Sarah': {
'shepherd': [1, 2, 3],
'collie': [3, 31, 4],
'poodle': [21, 5, 6],
},
'Ann': {
'shepherd': [4, 6, 3],
'collie': [23, 3, 45],
'poodle': [2, 10, 8],
}
}
print( flatten(dict1) )
You’re trying to use variables that don’t exist.
Use this
dict_flatted = [ i for names in dict1.values() for dog in names.values() for i in dog]
The recursive function can be single line:
def flatten(d):
return d if isinstance(d, list) else sum((flatten(i) for i in d.values()), [])
Nota: was an answer for a duplicate question
I started with @DaewonLee’s code and extended it to more data types and to recurse within lists:
def flatten(d):
res = [] # type:list # Result list
if isinstance(d, dict):
for key, val in d.items():
res.extend(flatten(val))
elif isinstance(d, list):
for val in d:
res.extend(flatten(val))
elif isinstance(d, (float, str, int)):
res = [d] # type: List[float] or List[str] or List[int]
else:
raise TypeError("Undefined type for flatten: %s" % type(d))
return res
for this specific case: (2 levels)
-
using itertools.chain method:
nested_dict = {1 : {‘a’ : [1,2,3], ‘b’ : [4,5]},
2 : {‘a’ : [6], ‘b’ : [7,8,9]}}list(chain(*[v for y in nested_dict.values() for v in list(y.values())]))
[1, 2, 3, 4, 5, 6, 7, 8, 9]
using pandas:
df = pd.DataFrame(pd.json_normalize(data=nested_dict), dtype=object)
df['chain'] = df.apply(lambda x: list(chain(*x.values.tolist())), axis=1)