I want to create a function to recursively traverse a multidimensional dictionary, where the dimensions are unknown.
Here is what I have come up with so far, but it doesn't seem to be working correctly. This will print out some key / values twice and they are not in order.
def walk_dict(d):
for k,v in d.items():
if isinstance(v, dict):
walk_dict(v)
else:
print "%s %s" % (k, v)
Here's a sample array:
d = {
'plan_code': 'b',
'quantity': '1',
'account': {
'account_code': 'b',
'username': 'jdoe',
'email': 'jdoe@domain.com',
'first_name': 'b',
'last_name': 'b',
'company_name': 'Company, LLC.',
'billing_info': {
'first_name': 'b',
'last_name': 'b',
'address1': '123 Test St',
'city': 'San Francisco',
'state': 'CA',
'country': 'US',
'zip': '94105',
'credit_card': {
'number': '1',
'year': '2018',
'month': '12',
'verification_value': '123',
},
},
},
}