Answers for "nested dictionary in python"

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

python get nested dictionary keys

example_dict.get('key1', {}).get('key2')
Posted by: Guest on August-13-2021
0

nested dictionary in python

# Nested dictionary in python
students = {}  # Creating parent dictionary

students["std1"] = {}   # Creating std1 child dictionary
students["std2"] = {}    # Creating std2 child dictionary

students["std1"]["Name"] = "Vikram"     # Adding key value pair to std1
students["std1"]["Age"] = 16         # Adding key value pair to std1
students["std1"]["Marks"] = 45      # Adding key value pair to std1

students["std2"]["Name"] = "Arjun"      # Adding key value pair to std2
students["std2"]["Age"] = 16           # Adding key value pair to std2
students["std2"]["Marks"] = 54        # Adding key value pair to std2


print(students)
Posted by: Guest on October-20-2021
0

nested dictionary in python

# Nested dictionary with user input
family = {}
num = int(input("How many child dictionaries do you want to create: "))

for i in range(num):
    Member = input("Enter child dictionary name: ")
    family[Member] = {}
    Name = input("Enter person name: ")
    Age = input("Enter person age: ")
    family[Member]["Name"] = Name
    family[Member]["Age"] = Age

print("_" * 20)
for i in family:
    print("Name",":",family[i]["Name"])
    print("Age",":",family[i]["Age"])
    print("_" * 20)
Posted by: Guest on October-20-2021
0

create nested dictionary in python

family = {
    "member1":{
        "Name":"Ajay",
        "Age":59,
        "Occupation":"Doctor",
        "Relation":"Dad"
        },
    
    "member2":{
        "Name":"Neha",
        "Age":57,
        "Occupation":"Housewife",
        "Relation":"Mom"
        },
    
    "member3":{
        "Name":"Dev",
        "Age":22,
        "Occupation":"Std",
        "Relation":"Son"
        },
    
    "member4":{
        "Name":"Vivek",
        "Age":21,
        "Occupation":"Std",
        "Relation":"Son"
        },
    
    "member5":{
        "Name":"Hana",
        "Age":16,
        "Occupation":"Std",
        "Relation":"Daughter"
        }
    }

print(family)  # Accessing parent dictionary

print("\n\n",family["member1"])  # Accessing child dictionary

print("\n\n",family["member1"]["Occupation"])  # Accessing child dictionary key
Posted by: Guest on October-19-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 "nested dictionary in python"

Python Answers by Framework

Browse Popular Code Answers by Language