
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
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
- Related Articles
- How to get a list of all the keys from a Python dictionary?
- How to create Python dictionary from list of keys and values?
- How to Common keys in list and dictionary using Python
- Get dictionary keys as a list in Python
- Python – Limit the values to keys in a Dictionary List
- How to sort a nested Python dictionary?
- Python - Convert list of nested dictionary into Pandas Dataframe
- How to create nested Python dictionary?
- How to recursively iterate a nested Python dictionary?
- Python - Create nested list containing values as the count of list items
- How to sort a dictionary in Python by keys?
- How to count elements in a nested Python dictionary?
- Recursively list nested object keys JavaScript
- How to print all the keys of a dictionary in Python
- How to add new keys to a dictionary in Python?

Advertisements