Add a key:value pair to dictionary in Python
Dictionary in Python is an unordered collection of data values, used to store data values like a map, which unlike other Data Types that hold only single value as an element, Dictionary holds key:value pair.
While using Dictionary, sometimes, we need to add or modify the key/value inside the dictionary. Let’s see how to add a key:value pair to dictionary in Python.
Code #1: Using Subscript notation
This method will create a new key:value pair on a dictionary by assigning a value to that key.
# Python program to add a key:value pair to dictionary dict = {'key1':'geeks', 'key2':'for'} print("Current Dict is: ", dict) # using the subscript notation # Dictionary_Name[New_Key_Name] = New_Key_Value dict['key3'] = 'Geeks'dict['key4'] = 'is'dict['key5'] = 'portal'dict['key6'] = 'Computer'print("Updated Dict is: ", dict) |
Current Dict is: {‘key2’: ‘for’, ‘key1’: ‘geeks’}
Updated Dict is: {‘key3’: ‘Geeks’, ‘key5’: ‘portal’, ‘key6’: ‘Computer’, ‘key4’: ‘is’, ‘key1’: ‘geeks’, ‘key2’: ‘for’}
Code #2: Using update() method
dict = {'key1':'geeks', 'key2':'for'} print("Current Dict is: ", dict) # adding dict1 (key3, key4 and key5) to dict dict1 = {'key3':'geeks', 'key4':'is', 'key5':'fabulous'} dict.update(dict1) # by assigning dict.update(newkey1 ='portal') print(dict) |
Current Dict is: {‘key2’: ‘for’, ‘key1’: ‘geeks’}
{‘newkey1’: ‘portal’, ‘key4’: ‘is’, ‘key2’: ‘for’, ‘key1’: ‘geeks’, ‘key5’: ‘fabulous’, ‘key3’: ‘geeks’}
Code #3: Taking Key:value as input
# Let's add key:value to a dictionary, the functional way # Create your dictionary class class my_dictionary(dict): # __init__ function def __init__(self): self = dict() # Function to add key:value def add(self, key, value): self[key] = value # Main Function dict_obj = my_dictionary() # Taking input key = 1, value = Geek dict_obj.key = input("Enter the key: ") dict_obj.value = input("Enter the value: ") dict_obj.add(dict_obj.key, dict_obj.value) dict_obj.add(2, 'forGeeks') print(dict_obj) |
Output:
{'1': 'Geeks', 2: 'forGeeks'}
Recommended Posts:
- Python | Convert byteString key:value pair of dictionary to String
- Python | Convert key-value pair comma separated string into dictionary
- Python | Convert nested dictionary into flattened dictionary
- Python | Convert flattened dictionary into nested dictionary
- Python | Convert string dictionary to dictionary
- Python Dictionary
- Python | Get key from value in Dictionary
- Python | Nested Dictionary
- Python | Add new keys to a dictionary
- Python | Dictionary with index as value
- Second largest value in a Python Dictionary
- Python Dictionary | pop() method
- Python | Get key with maximum value in Dictionary
- Iterate over a dictionary in Python
- Few mistakes when using Python dictionary
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



