I\'m trying to flatten the nested dictionary:
dict1 = {
\'Bob\': {
\'shepherd\': [4, 6, 3],
\'collie\': [23, 3, 45],
\'poodle\':
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):
res = [d] # type: List[float]
elif isinstance(d, str):
res = [d] # type: List[str]
elif isinstance(d, int):
res = [d] # type: List[int]
else:
raise TypeError("Undefined type for flatten: %s" % type(d))
return res