• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / Rename Key in Dictionary in Python

Rename Key in Dictionary in Python

June 6, 2022 Leave a Comment

When working with dictionaries in Python, to rename a key, you can use the dictionary pop() function.

dictionary = {"apples":3, "bananas":4, "pears":5}

dictionary["watermelon"] = dictionary.pop("apples")

print(dictionary)

#Output:
{"bananas":4, "pears":5, "watermelon":3}

You can also use the del keyword to rename a key in a dictionary variable in Python.

dictionary = {"apples":3, "bananas":4, "pears":5}

dictionary["watermelon"] = dictionary["apples"]

del dictionary["apples"]

print(dictionary)

#Output:
{"bananas":4, "pears":5, "watermelon":3}

In Python, dictionaries are a collection of key/value pairs separated by commas. When working with dictionaries, it can be useful to be able to easily access certain elements to rename their values.

We can easily rename keys in a Python dictionary.

To rename a key in a Python dictionary, you can use the dictionary pop() function to pop a key/value pair from the dictionary and assign it to a new key.

Below shows you an example of how to rename a key in a Python dictionary.

dictionary = {"apples":3, "bananas":4, "pears":5}

dictionary["watermelon"] = dictionary.pop("apples")

print(dictionary)

#Output:
{"bananas":4, "pears":5, "watermelon":3}

Using del to Rename Key in Python Dictionary Variable

Another way you can rename a key is with the Python del keyword. In this case, you will create a new key and then delete the key you want to change in your dictionary variable.

Below shows you how to change a key using the Python del keyword.

dictionary = {"apples":3, "bananas":4, "pears":5}

dictionary["watermelon"] = dictionary["apples"]

del dictionary["apples"]

print(dictionary)

#Output:
{"bananas":4, "pears":5, "watermelon":3}

Replacing Values in Python Dictionary

If you are want to replace a value in a dictionary in Python, then you just need to access it with the key name and reassign it with the new value.

Below is an example in Python of how to reassign and replace a value in a dictionary.

dictionary = {"apples":3, "bananas":4, "pears":5}

dictionary["apples"] = 6

print(dictionary)

#Output:
{"apples":6, "bananas":4, "pears":5}

Hopefully this article has been useful for learning how to replace values in dictionaries using Python.

Other Articles You'll Also Like:

  • 1.  Using Python to Add Trailing Zeros to String
  • 2.  Check if Variable is None in Python
  • 3.  Python max float – What’s the Maximum Float Value in Python?
  • 4.  Euclidean Algorithm and Extended Euclidean Algorithm in Python
  • 5.  Using Python to Replace Multiple Spaces with One Space in String
  • 6.  Reverse String with Recursion in Python
  • 7.  Change Python Turtle Shape Fill Color with fillcolor() Function
  • 8.  How to Remove NaN from List in Python
  • 9.  pi in Python – Using Math Module and Leibniz Formula to Get Value of pi
  • 10.  Using Python to Find Second Smallest Value in List

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

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

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy