PYTHON NESTED DICTIONARIES
INTRODUCTION
Key and value pair both can be of different data type in python dictionaries. Keys can be of string data type and values can be of complex data type.
Accessing Values
Using Keys
# PYTHON NESTED DICTIONARIES
# Dictionary nested value example 1
dict_class_wise_stud_list = {'A':[100, 101, 102], 'B':[200, 201, 202], 'C':[300, 301, 302], 'D':[400, 401, 402], 'E':[500, 501, 502], 'F':[600, 601, 602]}
print("\nList of key value pair of dict_class_wise_stud_list ->", dict_class_wise_stud_list)
#Accessing values against keys
print("\nValue of key A in dict_class_wise_stud_list ->", dict_class_wise_stud_list['A'])
print("Value of key B in dict_class_wise_stud_list ->", dict_class_wise_stud_list['B'])
print("Value of key C in dict_class_wise_stud_list ->", dict_class_wise_stud_list['C'])
print("Value of key D in dict_class_wise_stud_list ->", dict_class_wise_stud_list['D'])
print("Value of key E in dict_class_wise_stud_list ->", dict_class_wise_stud_list['E'])
print("Value of key F in dict_class_wise_stud_list ->", dict_class_wise_stud_list['F'])
PYTHON NESTED DICTIONARIES : Output
List of key value pair of dict_class_wise_stud_list -> {'A': [100, 101, 102], 'B': [200, 201, 202], 'C': [300, 301, 302], 'D': [400, 401, 402], 'E': [500, 501, 502], 'F': [600, 601, 602]}
Value of key A in dict_class_wise_stud_list -> [100, 101, 102]
Value of key B in dict_class_wise_stud_list -> [200, 201, 202]
Value of key C in dict_class_wise_stud_list -> [300, 301, 302]
Value of key D in dict_class_wise_stud_list -> [400, 401, 402]
Value of key E in dict_class_wise_stud_list -> [500, 501, 502]
Value of key F in dict_class_wise_stud_list -> [600, 601, 602]
In the above example, we have used list item as values which is a complex data type.
Using values() function
# PYTHON NESTED DICTIONARIES
# Dictionary nested value example 2
dict_class_wise_stud_list = {'A':[100, 101, 102], 'B':[200, 201, 202], 'C':[300, 301, 302], 'D':[400, 401, 402], 'E':[500, 501, 502], 'F':[600, 601, 602]}
print("\nList of key value pair of dict_class_wise_stud_list ->", dict_class_wise_stud_list)
#Accessing values against keys
print("\nList of values of dict_class_wise_stud_list ->", dict_class_wise_stud_list.values())
PYTHON NESTED DICTIONARIES : Output
List of key value pair of dict_class_wise_stud_list -> {'A': [100, 101, 102], 'B': [200, 201, 202], 'C': [300, 301, 302], 'D': [400, 401, 402], 'E': [500, 501, 502], 'F': [600, 601, 602]}
List of values of dict_class_wise_stud_list -> dict_values([[100, 101, 102], [200, 201, 202], [300, 301, 302], [400, 401, 402], [500, 501, 502], [600, 601, 602]])
In the above example, we have used the values() function to accessed values of dictionary dict_class_wise_stud_list.
Using list() function
#PYTHON NESTED DICTIONARIES
# Dictionary nested value example 3
dict_class_wise_stud_list = {'A':[100, 101, 102], 'B':[200, 201, 202], 'C':[300, 301, 302], 'D':[400, 401, 402], 'E':[500, 501, 502], 'F':[600, 601, 602]}
print("\nList of key value pair of dict_class_wise_stud_list ->", dict_class_wise_stud_list)
#Accessing values against keys
print("\nList of values of dict_class_wise_stud_list ->", dict_class_wise_stud_list.values())
print("\n1st element of 1st value of dict_class_wise_stud_list ->", list(dict_class_wise_stud_list.values())[0])
print("2nd element of 1st value of dict_class_wise_stud_list ->", list(dict_class_wise_stud_list.values())[1])
print("3rd element of 1st value of dict_class_wise_stud_list ->", list(dict_class_wise_stud_list.values())[2])
print("4th element of 1st value of dict_class_wise_stud_list ->", list(dict_class_wise_stud_list.values())[3])
print("5th element of 1st value of dict_class_wise_stud_list ->", list(dict_class_wise_stud_list.values())[4])
print("6th element of 1st value of dict_class_wise_stud_list ->", list(dict_class_wise_stud_list.values())[5])
PYTHON NESTED DICTIONARIES : Output
List of key value pair of dict_class_wise_stud_list -> {'A': [100, 101, 102], 'B': [200, 201, 202], 'C': [300, 301, 302], 'D': [400, 401, 402], 'E': [500, 501, 502], 'F': [600, 601, 602]}
List of values of dict_class_wise_stud_list -> dict_values([[100, 101, 102], [200, 201, 202], [300, 301, 302], [400, 401, 402], [500, 501, 502], [600, 601, 602]])
1st element of 1st value of dict_class_wise_stud_list -> [100, 101, 102]
2nd element of 1st value of dict_class_wise_stud_list -> [200, 201, 202]
3rd element of 1st value of dict_class_wise_stud_list -> [300, 301, 302]
4th element of 1st value of dict_class_wise_stud_list -> [400, 401, 402]
5th element of 1st value of dict_class_wise_stud_list -> [500, 501, 502]
6th element of 1st value of dict_class_wise_stud_list -> [600, 601, 602]
In the above example, we have accessed the nested elements of a value against a key.
#PYTHON NESTED DICTIONARIES
# Dictionary nested value example 4
dict_class_wise_stud_list = {'A':[100, 101, 102], 'B':[{'C': 200, 'D': 300}]}
print("\nList of key value pair of dict_class_wise_stud_list ->", dict_class_wise_stud_list)
#Accessing values against keys
print("\nList of values of dict_class_wise_stud_list ->", dict_class_wise_stud_list.values())
print("\n1st value of dict_class_wise_stud_list ->", list(dict_class_wise_stud_list.values())[0])
print("2nd value of dict_class_wise_stud_list ->", list(dict_class_wise_stud_list.values())[1])
#Accessing nested values against keys
print("\n1st element of 1st value of dict_class_wise_stud_list ->", list(dict_class_wise_stud_list.values())[0][0])
print("\n2nd element of 1st value of dict_class_wise_stud_list ->", list(dict_class_wise_stud_list.values())[0][1])
print("\n3rd element of 1st value of dict_class_wise_stud_list ->", list(dict_class_wise_stud_list.values())[0][2])
print("\n1st element of 2nd value of dict_class_wise_stud_list ->", list(dict_class_wise_stud_list.values())[1])
PYTHON NESTED DICTIONARIES : Output
List of key value pair of dict_class_wise_stud_list -> {'A': [100, 101, 102], 'B': [{'C': 200, 'D': 300}]}
List of values of dict_class_wise_stud_list -> dict_values([[100, 101, 102], [{'C': 200, 'D': 300}]])
1st value of dict_class_wise_stud_list -> [100, 101, 102]
2nd value of dict_class_wise_stud_list -> [{'C': 200, 'D': 300}]
1st element of 1st value of dict_class_wise_stud_list -> 100
2nd element of 1st value of dict_class_wise_stud_list -> 101
3rd element of 1st value of dict_class_wise_stud_list -> 102
1st element of 2nd value of dict_class_wise_stud_list -> [{'C': 200, 'D': 300}]
In the above example, we have used one list item and one inner dictionary in the outer dictionary. Then we have used the list() and values() function to access the values of dictionary dict_class_wise_stud_list.
Accessing Keys
Using keys() function
# Dictionary nested value example 5
dict_class_wise_stud_list = {'A':[100, 101, 102], 'B':[200, 201, 202], 'C':[300, 301, 302], 'D':[400, 401, 402], 'E':[500, 501, 502], 'F':[600, 601, 602]}
print("\nList of key value pair of dict_class_wise_stud_list ->", dict_class_wise_stud_list)
#Accessing keys
print("\nList of values of dict_class_wise_stud_list ->", dict_class_wise_stud_list.keys())
PYTHON NESTED DICTIONARIES : Output
List of key value pair of dict_class_wise_stud_list -> {'A': [100, 101, 102], 'B': [200, 201, 202], 'C': [300, 301, 302], 'D': [400, 401, 402], 'E': [500, 501, 502], 'F': [600, 601, 602]}
List of values of dict_class_wise_stud_list -> dict_keys(['A', 'B', 'C', 'D', 'E', 'F'])
In the above example, we have listed all the keys of dictionary dict_class_wise_stud_list by using keys() function.
Using list() function
# Dictionary nested value example 6
dict_class_wise_stud_list = {'A':[100, 101, 102], 'B':[200, 201, 202], 'C':[300, 301, 302], 'D':[400, 401, 402], 'E':[500, 501, 502], 'F':[600, 601, 602]}
print("\nList of key value pair of dict_class_wise_stud_list ->", dict_class_wise_stud_list)
#Accessing keys
print("\nList of values of dict_class_wise_stud_list ->", dict_class_wise_stud_list.keys())
print("\n1st key of dict_class_wise_stud_list ->", list(dict_class_wise_stud_list.keys())[0])
print("2nd key of dict_class_wise_stud_list ->", list(dict_class_wise_stud_list.keys())[1])
print("3rd key of dict_class_wise_stud_list ->", list(dict_class_wise_stud_list.keys())[2])
print("4th key of dict_class_wise_stud_list ->", list(dict_class_wise_stud_list.keys())[3])
print("5th key of dict_class_wise_stud_list ->", list(dict_class_wise_stud_list.keys())[4])
print("6th key of dict_class_wise_stud_list ->", list(dict_class_wise_stud_list.keys())[5])
PYTHON NESTED DICTIONARIES : Output
List of key value pair of dict_class_wise_stud_list -> {'A': [100, 101, 102], 'B': [200, 201, 202], 'C': [300, 301, 302], 'D': [400, 401, 402], 'E': [500, 501, 502], 'F': [600, 601, 602]}
List of values of dict_class_wise_stud_list -> dict_keys(['A', 'B', 'C', 'D', 'E', 'F'])
1st key of dict_class_wise_stud_list -> A
2nd key of dict_class_wise_stud_list -> B
3rd key of dict_class_wise_stud_list -> C
4th key of dict_class_wise_stud_list -> D
5th key of dict_class_wise_stud_list -> E
6th key of dict_class_wise_stud_list -> F
In the above example, we have used the list() function to print the individual key of dictionary dict_class_wise_stud_list.
Counting No of Keys
# Dictionary nested value example 7
dict_class_wise_stud_list = {'A':[100, 101, 102], 'B':[200, 201, 202], 'C':[300, 301, 302], 'D':[400, 401, 402], 'E':[500, 501, 502], 'F':[600, 601, 602]}
print("\nList of key value pair of dict_class_wise_stud_list ->", dict_class_wise_stud_list)
print("\nNumber of key value pair in dict_class_wise_stud_list ->", len(dict_class_wise_stud_list))
PYTHON NESTED DICTIONARIES : Output
List of key value pair of dict_class_wise_stud_list -> {'A': [100, 101, 102], 'B': [200, 201, 202], 'C': [300, 301, 302], 'D': [400, 401, 402], 'E': [500, 501, 502], 'F': [600, 601, 602]}
Number of key value pair in dict_class_wise_stud_list -> 6
In the above example, we have used len() function to print the number of key value pair in the dictionary dict_class_wise_stud_list.
# Dictionary nested value example 8
dict_class_wise_stud_list = {'A':[100, 101, 102], 'B':[200, 201, 202], 'C':[300, 301, 302], 'D':[400, 401, 402], 'E':[500, 501, 502], 'F':[600, 601, 602]}
print("\nList of key value pair of dict_class_wise_stud_list ->", dict_class_wise_stud_list)
print("\nNumber of key value pair in dict_class_wise_stud_list ->", len(dict_class_wise_stud_list))
print("\nList of elements in 1st value in dict_class_wise_stud_list ->", list(dict_class_wise_stud_list.values())[0])
print("\nNumber of elements in 1st value in dict_class_wise_stud_list ->", len(list(dict_class_wise_stud_list.values())[0]))
PYTHON NESTED DICTIONARIES : Output
List of key value pair of dict_class_wise_stud_list -> {'A': [100, 101, 102], 'B': [200, 201, 202], 'C': [300, 301, 302], 'D': [400, 401, 402], 'E': [500, 501, 502], 'F': [600, 601, 602]}
Number of key value pair in dict_class_wise_stud_list -> 6
List of elements in 1st value in dict_class_wise_stud_list -> [100, 101, 102]
Number of elements in 1st value in dict_class_wise_stud_list -> 3
In the above example, we have used both len() and list() function to count the element of list object used as values in dictionary dict_class_wise_stud_list.

RELATED TOPICS:
- INTRODUCTION TO PYTHON DICTIONARIES
- PYTHON NUMBER KEY VALUE IN DICTIONARY
- PYTHON STRING KEY VALUE IN DICTIONARY
- PYTHON MIXED KEY VALUE IN DICTIONARY
- ADDING NEW ITEMS IN PYTHON DICTIONARY
- UPDATING PYTHON DICTIONARIES
- REMOVING ITEMS FROM PYTHON DICTIONARIES
- PYTHON DICTIONARY SORTING OPERATIONS
- PYTHON DICTIONARY BUILT IN FUNCTIONS
- PYTHON DICTIONARY SHALLOW COPY OPERATION
- PYTHON DICTIONARY DEEP COPY OPERATION