INTRODUCTION TO PYTHON DICTIONARIES
INTRODUCTION
- Dictionaries are unordered collection of key value pair
- Each key in the dictionary is associated with a value
- Each key should be unique but values can be duplicate
- Values are searched with the help of corresponding keys
- Values can be of simple or complex data type
- Dictionaries are mutable. New keys can be inserted and values can be updated.
- Dictionaries are enclosed with curly brackets
DICTIONARY DATA TYPE
Dictionaries are enclosed with in curly brackets. Key and values are paired with semi colon. Each key value pair are separated with a comma.
# Dictionary data type example 1
dict_year_count = {2020:100, 2021:200, 2022:300, 2023:400, 2024:500, 2025:600}
print("\nData type of dict_year_count ->", type(dict_year_count))
INTRODUCTION TO PYTHON DICTIONARIES : Output
Data type of dict_year_count -> <class 'dict'>
NUMERIC KEY VALUE PAIR
INTEGER VALUE
# Dictionary data type example 2
dict_year_count = {2020:100, 2021:200, 2022:300, 2023:400, 2024:500, 2025:600}
print("\nList of key value pair of dict_year_count ->", dict_year_count)
INTRODUCTION TO PYTHON DICTIONARIES : Output
List of key value pair of dict_year_count -> {2020: 100, 2021: 200, 2022: 300, 2023: 400, 2024: 500, 2025: 600}
In the above example, key and values both are integer.
FLOATING VALUE
# Dictionary data type example 3
dict_year_count = {2020:10.5, 2021:15.2, 2022:30.7, 2023:25.9, 2024:27.6, 2025:19.4}
print("\nList of key value pair of dict_year_count ->", dict_year_count)
INTRODUCTION TO PYTHON DICTIONARIES : Output
List of key value pair of dict_year_count -> {2020: 10.5, 2021: 15.2, 2022: 30.7, 2023: 25.9, 2024: 27.6, 2025: 19.4}
In the above example, keys are integer and values are floating number.
STRING KEY VALUE PAIR
# Dictionary data type example 4
dict_prog_type = {'A':'Python', 'B':'Java', 'C':'C++', 'D':'PHP', 'E':'XML', 'F':'VB'}
print("\nList of key value pair of dict_prog_type ->", dict_prog_type)
INTRODUCTION TO PYTHON DICTIONARIES : Output
List of key value pair of dict_prog_type -> {'A': 'Python', 'B': 'Java', 'C': 'C++', 'D': 'PHP', 'E': 'XML', 'F': 'VB'}
In the above example, key and value both are string.
MIXED KEY VALUE PAIR
# Dictionary data type example 5
dict_section_count = {'A':100, 'B':200, 'C':300, 'D':400, 'E':500, 'F':600}
print("\nList of key value pair of dict_section_count ->", dict_section_count)
INTRODUCTION TO PYTHON DICTIONARIES : Output
List of key value pair of dict_section_count -> {'A': 100, 'B': 200, 'C': 300, 'D': 400, 'E': 500, 'F': 600}
In the above example, keys are string and values are integer.
# Dictionary data type example 6
dict_section_valid = {'A':True, 'B':True, 'C':False, 'D':True, 'E':False, 'F':True}
print("\nList of key value pair of dict_section_valid ->", dict_section_valid)
INTRODUCTION TO PYTHON DICTIONARIES : Output
List of key value pair of dict_section_valid -> {'A': True, 'B': True, 'C': False, 'D': True, 'E': False, 'F': True}
In the above example, keys are string and values are boolean.
NESTED DICTIONARIES
# Dictionary data type example 7
dict_class_student_list = {'A':100, 'B':[102, 104, 106], 'C':['John', 'Smith', 'Steve'], 'D':(110, 111, 112), 'E':115, 'F':{116:'Tom', 117:'Jerry'}}
print("\nList of key value pair of dict_class_student_list ->", dict_class_student_list)
INTRODUCTION TO PYTHON DICTIONARIES : Output
List of key value pair of dict_class_student_list -> {'A': 100, 'B': [102, 104, 106], 'C': ['John', 'Smith', 'Steve'], 'D': (110, 111, 112), 'E': 115, 'F': {116: 'Tom', 117: 'Jerry'}}
In the above example, we have used multiple complex data type as value. We have used integer list against key B, string list against key C, tuple against D and dictionary against key F.
DUPLICACY IN DICTIONARIES
Keys in the dictionaries can not be duplicate. But values can be duplicate. In case if we try to use duplicate keys in the dictionaries then python will use the last key value pair and others will be removed..
# Dictionary data type example 8
dict_section_count = {'A':100, 'B':200, 'C':300, 'D':400, 'E':500, 'F':600, 'C':700}
print("\nList of key value pair of dict_section_count ->", dict_section_count)
dict_year_count = {2020:100, 2021:200, 2022:300, 2023:400, 2024:500, 2025:600, 2023:700}
print("\nList of key value pair of dict_year_count ->", dict_year_count)
INTRODUCTION TO PYTHON DICTIONARIES : Output
List of key value pair of dict_section_count -> {'A': 100, 'B': 200, 'C': 700, 'D': 400, 'E': 500, 'F': 600}
List of key value pair of dict_year_count -> {2020: 100, 2021: 200, 2022: 300, 2023: 700, 2024: 500, 2025: 600}
In the above example, we have used duplicate key C for dictionary dict_section_count. But python has taken the last duplicate key that’s why the output is showing C:700. Similarly, we have used duplicate key 2023 for dictionary dict_year_count, but python has taken the last key value pair and output is showing 2023:700.

RELATED TOPICS:
- PYTHON NUMBER KEY VALUE IN DICTIONARY
- PYTHON STRING KEY VALUE IN DICTIONARY
- PYTHON MIXED KEY VALUE IN DICTIONARY
- PYTHON NESTED DICTIONARIES
- 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
Pingback: PYTHON NUMBER KEY VALUE IN DICTIONARY - Sayantan's Blog On Python Programming