I want to create a recursive method with Python that prints this dictionary:
partners = {
'manager-1': {
'name': 'Manager 1',
'children': {
'manager-2': {
'name': 'Manager 2',
'children': {
'employee-1': {
'name': 'Employee 1',
'children': {
'employee-7': {
'name': 'Employee 7',
},
'employee-8': {
'name': 'Employee 8',
}
}
},
'employee-2': {
'name': 'Employee 2',
},
'employee-3': {
'name': 'Employee 3',
},
},
},
'manager-3': {
'name': 'Manager 3',
'children': {
'employee-4': {
'name': 'Employee 4',
},
'employee-5': {
'name': 'Employee 5',
},
'employee-6': {
'name': 'Employee 6',
},
},
},
'manager-4': {
'name': 'Manager 4',
},
'manager-5': {
'name': 'Manager 5',
}
}
}
}
And gives it like this:
--Manager 1
----Manager 2
------Employee 1
--------Employee 7
--------Employee 8
------Employee 2
------Employee 3
----Manager 3
------Manager 4
------Manager 5
------Manager 6
----Manager 4
----Manager 5
I made this method:
def hierarch(partners):
for partner in partners:
if 'children' not in partner.keys():
print(partner['name'])
else:
hierarch(partner['children'])
And I got this error when running the code:
AttributeError: 'str' object has no attribute 'keys'
How can I make a script that allows me to print this hierarchy? I'm not really good with dictionaries. I'm working with Python 3. Any help please? Thanks.
Employee 7
?