Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Selected Reading
How to access nested Python dictionary items via a list of keys?
The easiest and most readable way to access nested properties in a Python dict is to use for loop and loop over each item while getting the next value, until the end.
example
def getFromDict(dataDict, mapList):
for k in mapList: dataDict = dataDict[k]
return dataDict
a = {
'foo': 45,'bar': {
'baz': 100,'tru': "Hello"
}
}
print(getFromDict(a, ["bar", "baz"]))
Output
This will give the output −
100
Advertisements
