Answers for "python create nested dictionary"

2

Nested dictionary Python

IDs = ['emp1','emp2','emp3']

EmpInfo = [{'name': 'Bob', 'job': 'Mgr'},
           {'name': 'Kim', 'job': 'Dev'},
           {'name': 'Sam', 'job': 'Dev'}]

D = dict(zip(IDs, EmpInfo))

print(D)
# Prints {'emp1': {'name': 'Bob', 'job': 'Mgr'},
#         'emp2': {'name': 'Kim', 'job': 'Dev'},
#         'emp3': {'name': 'Sam', 'job': 'Dev'}}
Posted by: Guest on November-08-2020
0

make nested dict from two dict

data = [
    [14, 77766, [2, 2]],
    [15, 77766, [1, 2]],
    [70, 88866, [1, 5]],
    [71, 88866, [2, 5]],
    [72, 88866, [5, 5]],
    [73, 88866, [4, 5]],
    [74, 88866, [3, 5]],
    [79, 99966, [1, 2]],
    [80, 99966, [2, 2]],
]

c = {}
for key, id_, (value, _) in data:
    c.setdefault(id_, {})[key] = value
print(c)
Posted by: Guest on January-02-2021
0

create nested dictionary in python

family = { }  # empty nested dictionary
print("\n",family)

family['member1'] = {}  # adding child dictionary
print("\n",family)


family['member1']['name'] = 'Bob'   # Adding elements one at a time
family['member1']['age'] = 21     # Adding elements one at a time

print("\n",family)  # After adding member1 dictionary 


family['member2'] = {'name': 'Cara', 'age': 25}  # Adding whole member2 dictionary

print("\n",family)
Posted by: Guest on October-19-2021

Code answers related to "python create nested dictionary"

Python Answers by Framework

Browse Popular Code Answers by Language