ADDING NEW ITEMS IN PYTHON DICTIONARY
INTRODUCTION
Python has provided us a group of built-in methods for Dictionary manipulation. Below are the methods for adding new items in the dictionary.
Assignment Operator
# ADDING NEW ITEMS IN PYTHON DICTIONARY
# Example 1
dict_year_count = {2020:100, 2021:200, 2022:300}
print("\nList of key value pair of dict_year_count ->", dict_year_count)
dict_year_count[2023] = 900
print("\nList of key value pair of dict_year_count ->", dict_year_count)
ADDING NEW ITEMS IN PYTHON DICTIONARY : Output
List of key value pair of dict_year_count -> {2020: 100, 2021: 200, 2022: 300}
List of key value pair of dict_year_count -> {2020: 100, 2021: 200, 2022: 300, 2023: 900}
In the above example, we have used assignment operator to add a new key value pair in the dictionary dict_year_count.
# ADDING NEW ITEMS IN PYTHON DICTIONARY
# Example 2
dict_year_count = {2020:100, 2021:200, 2022:300}
print("\nList of key value pair of dict_year_count ->", dict_year_count)
dict_year_count[2023] = 900
print("\nList of key value pair of dict_year_count ->", dict_year_count)
print("\nNew key of dict_year_count ->", list(dict_year_count.keys())[3])
print("\nNew value against new key of dict_year_count ->", list(dict_year_count.values())[3])
ADDING NEW ITEMS IN PYTHON DICTIONARY : Output
List of key value pair of dict_year_count -> {2020: 100, 2021: 200, 2022: 300}
List of key value pair of dict_year_count -> {2020: 100, 2021: 200, 2022: 300, 2023: 900}
New key of dict_year_count -> 2023
New value against new key of dict_year_count -> 900
In the above example, we have accessed the newly added key and value separately.
# ADDING NEW ITEMS IN PYTHON DICTIONARY
# Example 3
dict_class_wise_prog = {'A':'Python', 'B':'Java', 'C':'C++'}
print("\nList of key value pair of dict_class_wise_prog ->", dict_class_wise_prog)
dict_class_wise_prog['D'] = 'PHP'
dict_class_wise_prog['E'] = 'XML'
print("\nList of key value pair of dict_class_wise_prog ->", dict_class_wise_prog)
print("\n3rd new key of dict_class_wise_prog ->", list(dict_class_wise_prog.keys())[3])
print("4th new key of dict_class_wise_prog ->", list(dict_class_wise_prog.keys())[4])
print("\n3rd new value against 3rd new key of dict_class_wise_prog ->", list(dict_class_wise_prog.values())[3])
print("4th new value against 4th new key of dict_class_wise_prog ->", list(dict_class_wise_prog.values())[4])
ADDING NEW ITEMS IN PYTHON DICTIONARY : Output
List of key value pair of dict_class_wise_prog -> {'A': 'Python', 'B': 'Java', 'C': 'C++'}
List of key value pair of dict_class_wise_prog -> {'A': 'Python', 'B': 'Java', 'C': 'C++', 'D': 'PHP', 'E': 'XML'}
3rd new key of dict_class_wise_prog -> D
4th new key of dict_class_wise_prog -> E
3rd new value against 3rd new key of dict_class_wise_prog -> PHP
4th new value against 4th new key of dict_class_wise_prog -> XML
Using update() method
# ADDING NEW ITEMS IN PYTHON DICTIONARY
# Example 3
dict_year_count = {2020:100, 2021:200, 2022:300}
print("\nList of key value pair of dict_year_count ->", dict_year_count)
dict_year_count.update({2023: 900, 2024: 1000})
print("\nList of key value pair of dict_year_count ->", dict_year_count)
print("\n3rd new key of dict_year_count ->", list(dict_year_count.keys())[3])
print("4th new key of dict_year_count ->", list(dict_year_count.keys())[4])
print("\n3rd new value against 3rd new key of dict_year_count ->", list(dict_year_count.values())[3])
print("4th new value against 4th new key of dict_year_count ->", list(dict_year_count.values())[4])
ADDING NEW ITEMS IN PYTHON DICTIONARY : Output
List of key value pair of dict_year_count -> {2020: 100, 2021: 200, 2022: 300}
List of key value pair of dict_year_count -> {2020: 100, 2021: 200, 2022: 300, 2023: 900, 2024: 1000}
3rd new key of dict_year_count -> 2023
4th new key of dict_year_count -> 2024
3rd new value against 3rd new key of dict_year_count -> 900
4th new value against 4th new key of dict_year_count -> 1000
In the above example, we have added two new key value pair using update() method.
Using ** operator
** operator helps us to merge one dictionary data into other dictionary along with additional items.
# ADDING NEW ITEMS IN PYTHON DICTIONARY
# Example 4
dict_year_count = {2020:100, 2021:200, 2022:300}
print("\nList of key value pair of dict_year_count ->", dict_year_count)
dict_year_count_new = {**dict_year_count, **{2023: 900, 2024: 1000}}
print("\nList of key value pair of dict_year_count ->", dict_year_count_new)
print("\n3rd new key of dict_year_count_new ->", list(dict_year_count_new.keys())[3])
print("4th new key of dict_year_count_new ->", list(dict_year_count_new.keys())[4])
print("\n3rd new value against 3rd new key of dict_year_count_new ->", list(dict_year_count_new.values())[3])
print("4th new value against 4th new key of dict_year_count_new ->", list(dict_year_count_new.values())[4])
ADDING NEW ITEMS IN PYTHON DICTIONARY : Output
List of key value pair of dict_year_count -> {2020: 100, 2021: 200, 2022: 300}
List of key value pair of dict_year_count -> {2020: 100, 2021: 200, 2022: 300, 2023: 900, 2024: 1000}
3rd new key of dict_year_count_new -> 2023
4th new key of dict_year_count_new -> 2024
3rd new value against 3rd new key of dict_year_count_new -> 900
4th new value against 4th new key of dict_year_count_new -> 1000
In the above example, we have used the ** operator use create and new dictionary dict_year_count_new from dict_year_count along with two new key value pair.
RELATED TOPICS:
- INTRODUCTION TO PYTHON DICTIONARIES
- PYTHON NUMBER KEY VALUE IN DICTIONARY
- PYTHON STRING KEY VALUE IN DICTIONARY
- PYTHON MIXED KEY VALUE IN DICTIONARY
- PYTHON NESTED DICTIONARIES
- 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: REMOVING ITEMS FROM PYTHON DICTIONARIES - Sayantan's Blog On Python Programming