-1

How am I supposed to extract the value 1234 from "password"?

account = {
    'Dicky': {
        'password': 1234,
        'balance': {
            'USD': 10,
            'HKD': 10000
        }
    }
}
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
D cph
  • 31
  • 1
  • 3
    Do you mean extract? You could use `account["Dicky"]["password"]` – Heike Mar 10 '21 at 14:25
  • Does this answer your question? [Safe method to get value of nested dictionary](https://stackoverflow.com/questions/25833613/safe-method-to-get-value-of-nested-dictionary) – Tomerikoo Mar 10 '21 at 14:32

2 Answers2

2

to extract values form dict:

dict["key"] 

so in your case because there is a nested dict:

dict["1stkey"]["2ndkey"]

so:

account["Dicky"]["password"]
Leonardo Scotti
  • 1,069
  • 8
  • 21
0

In Python, a nested dictionary is a dictionary inside a dictionary. example:

nested_dict = { 'dictA': {'key_1': 'value_1'},
            'dictB': {'key_2': 'value_2'}}

so:

nested_dict['dict1']['key_A']--->'value_A'

this question

account = {
'Dicky': {
    'password': 1234,
    'balance': {
        'USD': 10,
        'HKD': 10000
        }
     }
 }

account["Dicky"]["password"]
print(account["Dicky"]["password"])
1234
Salio
  • 1,058
  • 10
  • 21