Python Tutorials
Basic

Data Structure and Data Types

String

List

Tuple

Set

Dictionary

Functions / Methods

Advance

Exception Handling

Python Change Dictionary Items


Change Dictionary Items

In a Python dictionary, you can use the key name of an item to change its value.

Example
company_dictionary = {
    "name": "Google", 
    "country": "USA", 
    "start_year": 1995
}

company_dictionary["start_year"] = 2000
print(company_dictionary)
{'name': 'Google', 'country': 'USA', 'start_year': 2000}

In the above example, we changed the start_year of the company from 1995 to 2000. Here we used the start_year key to change its value.


Update Dictionary

Using the update() function, you can update the existing dictionary item value or add a new item to the dictionary.

The update() function accepts input param as a key-value pair dictionary.

Example
company_dictionary = {
    "name": "Google", 
    "country": "USA", 
    "start_year": 1995
}

company_dictionary.update({"start_year": 2000, "sector": "Private"})
print(company_dictionary)
{'name': 'Google', 'country': 'USA', 'start_year': 2000, 'sector': 'Private'}

In the above example, we are updating the company_dictionary start_year item value and adding a new item.

Here, in the output, you can see the start_year of the company_dictionary new value is 2000 and sector is "Private".