Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

how to get key value in nested dictionary python

def recursive_items(dictionary):
    for key, value in dictionary.items():
        if type(value) is dict:
            yield from recursive_items(value)
        else:
            yield (key, value)

a = {'a': {1: {1: 2, 3: 4}, 2: {5: 6}}}

for key, value in recursive_items(a):
    print(key, value)
Comment

extract specific key values from nested dictionary

>>> [val.get('phone') for val in people.values()]
['4563', '9102', '2341']
Comment

extract specific key values from nested dictionary

>>> [people[i]['phone'] for i in people]
['9102', '2341', '4563']
Comment

extract specific tuple values from two different keys from nested dictionary

print data.keys()                                                # top level keys
>>> ['alpha', 'beta']

print [x.keys() for x in data.values()]                          # second level keys
>>> [[9, 2, 3, 5], [9, 2, 3, 5]]

print [y.keys() for x in data.values() for y in x.values()]      # third level keys
>>> [[1, 3, 6], [1, 2], [1, 3, 6], [1, 3, 6], [1, 3, 6], [1, 2], [1, 3, 6], [1, 3, 6]]

print [y.values() for x in data.values() for y in x.values()]    # third level values
>>> [[9.0, 4.0, 5.5], [1.1, 4.1], [9.1, 4.1, 5.1], [9.2, 4.4, 5.4], [9.2, 4.9, 5.0], [4.0, 7.9], [24, 89, 98], [9, 4, 5]]
Comment

extract specific key values from nested dictionary

l = []
for person in people:
    l.append(people[person]['phone'])

>>> l
['9102', '2341', '4563']
Comment

PREVIOUS NEXT
Code Example
Python :: channel unhiding command in discord.py 
Python :: bad request 400 heroku app 
Python :: check if element is in list 
Python :: Python use number twice without assignment 
Python :: get legend lables and handles from plot in matplotlib 
Python :: @methodclass in python 
Python :: how to find the no of user for a wifi using python for ubuntu 
Python :: looping through strings 
Python :: query first 5 element in django 
Python :: eia api python 
Python :: statsmodels fitted values 
Python :: midpoint circle drawing algorithm 
Python :: how to do formatting in python with format function 
Python :: join two strings python 
Python :: ensemble model using voting classifier 
Python :: pyhton comment 
Python :: jacobi iteration method python 
Python :: read csv in spark 
Python :: Using python-poppler 
Python :: concate the dataframe in pandas.. 
Python :: python normalized correlation 
Python :: python replace text 
Python :: python unittest multiple test cases 
Python :: TypeError: __init__(): incompatible constructor arguments. The following argument types are supported: 1. tensorflow.python._pywrap_file_io.BufferedInputStream(arg0: str, arg1: int) 
Python :: how to open youtube from google chrome browser instead of internet explorerwhen coding in python 
Python :: df to dict 
Python :: python use getcontext 
Python :: django request.data example 
Python :: iloc pandas 
Python :: python cursor placement 
ADD CONTENT
Topic
Content
Source link
Name
7+2 =