'Accesing low levels in python dict's
I have a dict that looks like this:
dict = {
"A": "1",
"B": [
{
"C": "2",
"D": {
"E": "3",
"F": "4"
}
]
}
My last try was this:
for x in dict["B"]
print(x["D"])
But as you can see, I get all keys and its values from the object D when my need is only get E.
I need access to E key and get the values (get 3) with for loop. Any way to get this goal?
Solution 1:[1]
First store the dictionary in a variable of a name other than dict. For my answer I have stored it in my_dict.
For accessing the value of "E" from my_dict, you can simple use this code below,
my_dict["B"][0]["D"]["E"]
There's no need for a for loop to be involved.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Zero |
