Monday 9 September 2019

nested dictionaries


Accessing Mixed dictionaries
students = {1: {'name': 'Ravi', 'age': '27', 'sex': 'Male'},
          2: {'name': 'Sita', 'age': '22', 'sex': 'Female'}}

print(students[1]['name'])
print(students[1]['age'])
print(students[1]['sex'])

 How to change or add elements in a nested dictionary?
students = {1: {'name': 'Ravi', 'age': '27', 'sex': 'Male'},
          2: {'name': 'Sita', 'age': '22', 'sex': 'Female'}}

students [3] = {}

students [3]['name'] = 'ram'
students [3]['age'] = '24'
students [3]['sex'] = 'Female'
students [3]['married'] = 'No'

print(students [3])

How to delete elements from a nested dictionary?

students = {1: {'name': 'ravi', 'age': '27', 'sex': 'Male'},
          2: {'name': 'sita', 'age': '22', 'sex': 'Female'},
          3: {'name': 'ram', 'age': '24', 'sex': 'Male', 'married': 'No'},
          4: {'name': 'krishna', 'age': '29', 'sex': 'Male', 'married': 'Yes'}}

del student[3]['married']
del student[4]['married']

print(student[3])
print(student[4])
How to iterate through a Nested dictionary?
students = {1: {'Name': 'Ravi', 'Age': '27', 'Sex': 'Male'},
          2: {'Name': 'Sita', 'Age': '22', 'Sex': 'Female'}}

for p_id, p_info in students.items():
    print("\nStudents ID:", p_id)
   
    for key in p_info:
        print(key + ':', p_info[key])


Next topic: bytes data type
Previous topic:dictionary data type
 range data type

You may like the following posts:
  Built-in Data types

Files with Exception handling

#Ask the user to take two input integer values var a,b try:     a=int(input("enter any integer value:"))     b=int(input(&qu...