Python - Extract values of Particular Key in Nested Values Last Updated : 30 Jan, 2025 Comments Improve Suggest changes Like Article Like Report We are given a nested dictionary we need to extract the values of particular key. For example, we are having a dictionary d = {'a': 1,'b': {'a': 2, 'c': 3},'d': [{'a': 4}, {'a': 5}]} we need to to extract values from it. so that output becomes [1, 5, 4, 2]. Using a StackWe can manually manage a stack to iterate through the nested dictionary and lists. Python d = {'a': 1,'b': {'a': 2, 'c': 3},'d': [{'a': 4}, {'a': 5}]} key = 'a' s = [d] values = [] # Loop to process the stack until it's empty while s: c = s.pop() if isinstance(c, dict): # Iterate through dictionary's key-value pairs for k, v in c.items(): if k == key: values.append(v) elif isinstance(v, (dict, list)): s.append(v) # If the element is a list elif isinstance(c, list): s.extend(c) print(values) Output[1, 5, 4, 2] Explanation:Code uses a stack to explore a nested dictionary/list, processing each element without recursion.It collects values of specified key from dictionaries adding nested dictionaries/lists to stack for further exploration.Using a QueueAlternatively we can use a queue to explore structure in a breadth-first manner. Python from collections import deque d = {'a': 1,'b': {'a': 2, 'c': 3},'d': [{'a': 4}, {'a': 5}]} key = 'a' q = deque([d]) values = [] # Loop to process the queue until it's empty while q: c = q.popleft() if isinstance(c, dict): # Iterate through the dictionary's key-value pairs for k, v in c.items(): # If the key matches if k == key: values.append(v) # If the value is a dictionary or list elif isinstance(v, (dict, list)): q.append(v) elif isinstance(c, list): q.extend(c) print(values) Output[1, 2, 4, 5] Explanation:Code uses a queue (deque) to traverse a nested dictionary/list structure level by level processing each element iteratively.It collects values of specified key from dictionaries and adds nested dictionaries/lists to queue for further exploration.Iterate Through StructureIf we know structure is only a few levels deep (e.g., a dictionary containing lists or other dictionaries) we can use direct iteration without recursion or extra data structures. Python d = {'a': 1, 'b': {'a': 2, 'c': 3}, 'd': [{'a': 4}, {'a': 5}]} key = 'a' values = [] for k, v in d.items(): if k == key: values.append(v) # If the value is a dictionary, iterate through its items elif isinstance(v, dict): for k2, v2 in v.items(): # If the key matches in the nested dictionary append the value if k2 == key: values.append(v2) # If the value is a list iterate through its elements elif isinstance(v, list): for item in v: if isinstance(item, dict): for k2, v2 in item.items(): if k2 == key: values.append(v2) print(values) Output[1, 2, 4, 5] Explanation:Iterates through the top-level dictionary checking for the specified key and exploring nested dictionaries or lists.Checks nested dictionaries and lists for key matches appending values to the result list. Comment More infoAdvertise with us Next Article Python - Extract values of Particular Key in Nested Values M manjeet_04 Follow Improve Article Tags : Python Python Programs Python list-programs Python dictionary-programs Practice Tags : python Similar Reads 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 Python - Extract selective keys' values Including Nested Keys Sometimes, while working with Python dictionaries, we can have a problem in which we need to extract selective keys' values. This problem has been solved earlier, but sometimes, we can have multiple nestings and certain keys may be present in inner records. This problem caters all the nestings for e 7 min read Python | Extract key-value of dictionary in variables Sometimes, while working with dictionaries, we can face a problem in which we may have just a singleton dictionary, i.e dictionary with just a single key-value pair, and require to get the pair in separate variables. This kind of problem can come in day-day programming. Let's discuss certain ways in 5 min read Extract Subset of Key-Value Pairs from Python Dictionary In this article, we will study different approaches by which we can Extract the Subset Of Key-Value Pairs From the Python Dictionary. When we work with Python dictionaries, it often involves extracting subsets of key-value pairs based on specific criteria. This can be useful for tasks such as filter 4 min read Python - Extract ith element of K key's value Given a dictionary, extract ith element of K key's value list. Input : test_dict = {'Gfg' : [6, 7, 3, 1], 'is' : [9, 1, 4], 'best' : [10, 7, 4]}, K = 'Gfg', i = 1 Output : 7 Explanation : 1st index of 'Gfg''s value is 7.Input : test_dict = {'Gfg' : [6, 7, 3, 1], 'is' : [9, 1, 4], 'best' : [10, 7, 4] 6 min read Like