Customize Consent Preferences

We use cookies to help you navigate efficiently and perform certain functions. You will find detailed information about all cookies under each consent category below.

The cookies that are categorized as "Necessary" are stored on your browser as they are essential for enabling the basic functionalities of the site. ... 

Always Active

Necessary cookies are required to enable the basic features of this site, such as providing secure log-in or adjusting your consent preferences. These cookies do not store any personally identifiable data.

No cookies to display.

Functional cookies help perform certain functionalities like sharing the content of the website on social media platforms, collecting feedback, and other third-party features.

No cookies to display.

Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics such as the number of visitors, bounce rate, traffic source, etc.

No cookies to display.

Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.

No cookies to display.

Advertisement cookies are used to provide visitors with customized advertisements based on the pages you visited previously and to analyze the effectiveness of the ad campaigns.

No cookies to display.

python

How to add keys and values to a dictionary in Python

In this tutorial, we are going to see how to add keys and values to a dictionary in Python. Dictionary in Python is an unordered collection of values, which maps one set of objects(keys) to another set of objects(values) unlike other data types containing only one value as an element.

When using a dictionary, it is sometimes necessary to add or modify the key or value. In this tutorial, we are going to see how to add keys and values to a dictionary in Python.
 
[st_adsense]  

How to add keys and values to a dictionary

update() function in Python allows you to add items to a dictionary if the key is not in the dictionary. If the key is in the dictionary, it updates the key with the new value.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
# Dictionary of languages
languages_dic = {
"Python": 55,
"Java" : 142,
"PHP" : 32,
"C" : 13,
}
# Adding a new key-value pair
languages_dic.update( {'C++' : 24} )
#Display the update
print(languages_dic)
# Dictionary of languages languages_dic = { "Python": 55, "Java" : 142, "PHP" : 32, "C" : 13, } # Adding a new key-value pair languages_dic.update( {'C++' : 24} ) #Display the update print(languages_dic)
# Dictionary of languages
languages_dic = {
    "Python": 55,
    "Java" : 142,
    "PHP" : 32,
    "C" : 13,
}
# Adding a new key-value pair
languages_dic.update( {'C++' : 24} )
#Display the update 
print(languages_dic)

Output:

{'C++': 24, 'PHP': 32, 'Python': 55, 'C': 13, 'Java': 142}
[st_adsense]  

Update the value of an existing key in the dictionary

If we call the update() function with a key/value and the key already exists in the dictionary, its value will be updated with the new value. In the code below, we will update the value of the key ‘Python’:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
# Dictionary of languages
languages_dic = {
"Python": 55,
"Java" : 142,
"PHP" : 32,
"C" : 13,
}
# Updating the value
languages_dic.update(Python = 70)
#Display the update
print(languages_dic)
# Dictionary of languages languages_dic = { "Python": 55, "Java" : 142, "PHP" : 32, "C" : 13, } # Updating the value languages_dic.update(Python = 70) #Display the update print(languages_dic)
# Dictionary of languages
languages_dic = {
    "Python": 55,
    "Java" : 142,
    "PHP" : 32,
    "C" : 13,
}
# Updating the value
languages_dic.update(Python = 70)
#Display the update 
print(languages_dic)

Output:

{'Python': 70, 'PHP': 32, 'Java': 142, 'C': 13}
mcq

Leave a Reply

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