Open In App

Check and Update Existing Key in Python Dictionary

Last Updated : 23 Jul, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

The task of checking and updating an existing key in a Python dictionary involves verifying whether a key exists in the dictionary and then modifying its value if it does. If the key is found, its value can be updated to a new value, if the key is not found, an alternative action can be taken, such as adding a new key-value pair or leaving the dictionary unchanged.

For example, consider the dictionary d = {'name': 'Geek', 'rank': 10, 'city': 'Geek Town'}. If we want to update the 'rank' key to 'age', we check if 'rank' exists in the dictionary. If it does, we assign its value 10 to the new key 'age' and then remove 'rank' from the dictionary. The result would be d = {'name': 'Geek', 'city': 'Geek Town', 'age': 10}.

Using dict.get()

get() is the efficient way to retrieve the value associated with a key. It avoids raising a KeyError if the key does not exist and making it ideal for checking and updating keys. If the key exists, its value is returned, otherwise it returns None or a default value if provided.

Python
d = {'name': 'Geek', 'rank': 10, 'city': 'Geek Town'}

age = d.get('rank')
if age is not None:
    d['age'] = age
    del d['rank']

print(d)

Output
{'name': 'Geek', 'city': 'Geek Town', 'age': 10}

Explanation:

  • age = d.get('rank') retrieves the value of 'rank' safely, returning None if the key doesn't exist, avoiding a KeyError.
  • if age is not None checks if 'rank' exists in the dictionary.
  • d['age'] = age assigns the value of 'rank' stored in age to a new key 'age'.
  • del d['rank'] removes the 'rank' key after its value has been assigned to 'age'.

Using dict.setdefault()

setdefault() provides a way to access a key’s value and if the key does not exist, sets it to a default value. While this method is not as commonly used for updating, it can be used for checking and updating keys. In combination with pop(), it provides an efficient way to modify keys and remove them in one step.

Python
d = {'name': 'Geek', 'rank': 10, 'city': 'Geek Town'}

if 'rank' in d:
    d['age'] = d.pop('rank')

print(d)

Output
{'name': 'Geek', 'city': 'Geek Town', 'age': 10}

Explanation:

  • if 'rank' in d checks if the 'rank' key exists in the dictionary.
  • d['age'] = d.pop('rank') removes 'rank' and assigns its value to the new key 'age'.

Using in keyword

This is one of the most straightforward ways to check if a key exists in a dictionary. By using the in keyword, we can check for the presence of a key and then perform direct assignment and deletion operations.

Python
d = {'name': 'Geek', 'rank': 10, 'city': 'Geek Town'}

if 'rank' in d:
    d['age'] = d['rank']
    del d['rank']

print(d)

Output
{'name': 'Geek', 'city': 'Geek Town', 'age': 10}

Explanation:

  • if 'rank' in d checks if 'rank' exists in the dictionary.
  • d['age'] = d['rank'] assigns 'rank''s value to 'age'.
  • del d['rank'] removes the 'rank' key from the dictionary.

Using dictionary comprehension

Dictionary comprehension provides a more functional approach to creating or modifying dictionaries. While concise, this method creates a new dictionary in memory, which can be inefficient, especially with large dictionaries. It is not ideal for checking and updating a single key due to the overhead of creating a new dictionary.

Python
d = {'name': 'Geek', 'rank': 10, 'city': 'Geek Town'}

d = {key: (d['rank'] if key == 'rank' else value) for key, value in d.items()}

if 'rank' in d:
    d['age'] = d.pop('rank')

print(d)

Output
{'name': 'Geek', 'city': 'Geek Town', 'age': 10}

Explanation:

  • dictionary comprehension replaces rank's value with 10 while keeping other values unchanged.
  • if 'rank' in d checks if 'rank' still exists it won't after the comprehension .
  • d['age'] = d.pop('rank') moves 'rank''s value to 'age' and removes 'rank' from the dictionary.

Similar Reads