How Can I Add New Keys To Dictionary Python

Are you searching for a method to add new keys to the dictionary in Python? In this article, I will show you how to add a new key to Dictionary.

If you are using add() method in the dictionary then it might not work. Instead, you need to use the array methodology or you need to use the update() method to add or update any keys in the dictionary.

How to Add Keys to Dictionary In Python

It is really simple to add keys to Dictionary. Assume key as an index of the array and hence just add the value to that key in Dictionary. Below is the Syntax that you can use to add a new key or update the value of the existing key in the dictionary.

Syntax:
dictionary[key] = value
or
dictionary.update({key:value})
or 
dictionary.update(key=value)
or
dictionary.update(dict(key=value))

Let us see in the below example code how to add or update keys in the dictionary when you are using Python as the programming language.

#Initializing the Empty Dictionary
newDict = {}

#Adding new Key in Dictionary
newDict["key1"] = 10

#Printing value of Key from Dictionary
print("The Value from the dictionary Key is: {}".format(newDict["key1"]))

Output:

The Value from the dictionary Key is: 10

Create Non Empty Dictionary and Update or Add or Insert Keys to it

In this example, I will initialize the dictionary with key-value pairs and then I will further show you how you can add or update the key in the existing or already initialized dictionary.

#Initializing the Non-Empty Dictionary
newDict = {'A':1, 'B':2, 'C':5}

#Adding new Key in Dictionary
newDict['D'] = 4

#Printing value of Key from Dictionary
print("The Value from the dictionary Key {} is: {}".format('D', newDict['D']))

#updating the already Existing Key in Dictionary
newDict['C'] = 3

#Printing value of Key from Dictionary
print("The Value from the dictionary Key {} is: {}".format('C', newDict['C']))

Output:

The Value from the dictionary Key D is: 4
The Value from the dictionary Key C is: 3

As you can see in the above code example, I was able to successfully add or insert new key as ‘D’ and update the already existing key ‘C’ in the above Dictionary.

Check if Key Already Exists

If you want to check if Key already exists or not in the key and then only you want to add or update the key-value then you can use the if condition to check it as shown in the below example code.

#Initializing the Non-Empty Dictionary
newDict = {'A':1, 'B':2, 'C':5}

#Checking if 'D' exists or not in Dictionary
if 'D' not in newDict:
    #Adding new Key in Dictionary
    newDict['D'] = 4

#Printing value of Key from Dictionary
print("The Value from the dictionary Key {} is: {}".format('D', newDict['D']))

#Skiping the Update if Key Already Exists
if 'C' not in newDict:
    newDict['C'] = 3

#Printing value of Key from Dictionary
print("The Value from the dictionary Key {} is: {}".format('C', newDict['C']))

Output:

The Value from the dictionary Key D is: 4
The Value from the dictionary Key C is: 5

Using Update Method to Insert or Update the Key Value Pair in Dictionary

You can use the update method present in the Dictionary Class to update the value of the key in the dictionary if already present. The update method also inserts the value automatically if the given key is not found in the current Dictionary.

Let see the update method in the below example code and how you can use it to insert or update the keys or values of the dictionary.

#Initializing the Non-Empty Dictionary
newDict = {'A':1, 'B':2, 'C':5}

#Checking if 'D' exists or not in Dictionary
if 'D' not in newDict:
    #Adding new Key in Dictionary
    newDict.update(D=4)

#Printing value of Key from Dictionary
print("The Value from the dictionary Key {} is: {}".format('D', newDict['D']))

#Update if Key Already Exists
if 'C' in newDict:
    newDict.update({'C':3})
    #newDict.update(dict(C=3))
    #newDict.update(C=3) 


#Printing value of Key from Dictionary
print("The Value from the dictionary Key {} is: {}".format('C', newDict['C']))

Output:

The Value from the dictionary Key D is: 4
The Value from the dictionary Key C is: 3
How Can I Add New Keys To Dictionary Python

The update method is the best practice that is used in the industry and it is recommended that you use the update method in your code to insert or add or update the key in the dictionary.

Wrap Up

I hope that I was able to answer your query related to “How Can I Add new Keys to Dictionary”. If you have any better answers than the above suggested then I would be happy to see those in the comment section.

And if you have any unique way to add or update keys in the dictionary, also the efficient way then your code can be sponsored here with credits.

If you liked the above tutorial then please follow us on Facebook and Twitter. Let us know the questions and answer you want to cover in this blog.

Further Read:

  1. Finding The Index Of An Item In A List Python
  2. How to Split a List in Python – 3 Ways
  3. How To Rewrite Python’s Max Function
  4. Python: How To Check If List Is Empty
  5. How To List All Files Of A Directory in Python

Leave a Reply

Your email address will not be published. Required fields are marked *