user3447653 user3447653 - 1 month ago 8
Python Question

Access nested key-values in Python dictionary

I have the following dictionary structure and Iam trying to access the

total_payments
field. It is like accessing key of keys in Python dictionary:

key: METTS MARK , value: {'salary': 365788, 'to_messages': 807, 'deferral_payments': 'NaN', 'total_payments': 1061827, 'exercised_stock_options': 'NaN', 'bonus': 600000, 'restricted_stock': 585062, 'shared_receipt_with_poi': 702, 'restricted_stock_deferred': 'NaN', 'total_stock_value': 585062, 'expenses': 94299, 'loan_advances': 'NaN', 'from_messages': 29, 'other': 1740, 'from_this_person_to_poi': 1, 'poi': False, 'director_fees': 'NaN', 'deferred_income': 'NaN', 'long_term_incentive': 'NaN', 'email_address': a@b.com', 'from_poi_to_this_person': 38}

Answer

It rather seems that values of your dictionay are dict, and not that the keys are dict. So you can just access as follows:

your_dict[KEY1]['total_payments']

Please also anonymize the email address in your example.

Comments