Get particular Nested level Items from Dictionary - Python Last Updated : 07 Feb, 2025 Comments Improve Suggest changes Like Article Like Report Our task is to extract all items at a particular depth level from a given nested dictionary,. This means retrieving only the key-value pairs that exist at a specific depth within the dictionary. For example: d = {'a': {'b': {'c': 5, 'd': 6}, 'e': 7}, 'f': 8} and if we want all key-value pairs at level 2 then the output should be: {'b': {'c': 5, 'd': 6}, 'e': 7}.Using reduce() from functoolIn this method we use reduce() to traverse the dictionary level by level extracting key-value pairs at the required depth. Python from functools import reduce d = {'a': {'b': {'c': 5, 'd': 6}, 'e': 7}, 'f': 8} lvl = 2 res = reduce(lambda d, _: {k: v for v in d.values() if isinstance(v, dict) for k, v in v.items()}, range(lvl - 1), d) print(res) Output{'b': {'c': 5, 'd': 6}, 'e': 7} Explanation:reduce() iterates lvl - 1 times and this ensures we extract key-value pairs at the correct level without going too deep.d.values() selects only nested dictionaries: we ignore non-dictionary values and focus on deeper levels.Dictionary comprehension {k: v for k, v in v.items()} this flattens the nested dictionaries while preserving structure.Using update() with IterationWe use a stack to traverse the dictionary iteratively and extract key-value pairs at the required depth. Python data = {'a': {'b': {'c': 5, 'd': 6}, 'e': 7}, 'f': 8} lvl, res = 2, {} stk = [(data, 0)] while stk: d, depth = stk.pop() if depth == lvl - 1: res.update({k: v for k, v in d.items() if isinstance(v, dict)}) for v in d.values(): if isinstance(v, dict): stk.append((v, depth + 1)) print(res) Output{'b': {'c': 5, 'd': 6}} Explanation:stk stores dictionaries along with their depth.If depth == lvl - 1 then we use update() to add key-value pairs where values are dictionaries.We traverse values adding nested dictionaries to stk with increased depth. Comment More infoAdvertise with us Next Article Get particular Nested level Items from Dictionary - Python manjeet_04 Follow Improve Article Tags : Python Python Programs Python dictionary-programs Practice Tags : python Similar Reads Python Get All Values from Nested Dictionary In this article, we will learn how we can Get all Values from Nested Dictionary in Python Programming. A nested Dictionary in Python is a dictionary that contains another dictionary as its values. Using this, we can create a nested structure where each of the key-value pairs is in the outer dictiona 5 min read Python - How to Iterate over nested dictionary ? In this article, we will discuss how to iterate over a nested dictionary in Python. Nested dictionary means dictionary inside a dictionary and we are going to see every possible way of iterating over such a data structure. Nested dictionary in use: {'Student 1': {'Name': 'Bobby', 'Id': 1, 'Age': 20} 3 min read Get first K items in dictionary = Python We are given a dictionary and a number K, our task is to extract the first K key-value pairs. This can be useful when working with large dictionaries where only a subset of elements is needed. For example, if we have: d = {'a': 1, 'b': 2, 'c': 3, 'd': 4} and K = 2 then the expected output would be: 2 min read Get Values of Particular Key in List of Dictionaries - Python We are given a list of dictionaries and a particular key. Our task is to retrieve the values associated with that key from each dictionary in the list. If the key doesn't exist in a dictionary, it should be ignored. For example, if we have the following list of dictionaries and we want to extract th 2 min read Three Level Nested Dictionary Python In Python, a dictionary is a built-in data type used to store data in key-value pairs. Defined with curly braces `{}`, each pair is separated by a colon `:`. This allows for efficient representation and easy access to data, making it a versatile tool for organizing information. What is 3 Level Neste 4 min read Like