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

The Programming Expert

Solving All of Your Programming Headaches

  • Home
  • Learn to Code
    • Python
    • JavaScript
  • Code Snippets
    • 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.  Get Size of File in Python with os.path.getsize() Function
  • 2.  Repeat String with * Operator in Python
  • 3.  Get pandas Index Values as List in Python
  • 4.  Apply Function to All Elements in List in Python
  • 5.  Replace Character in String in Python
  • 6.  pandas Absolute Value – Get Absolute Values in a Series or DataFrame
  • 7.  Using Python to Sum Odd Numbers in List
  • 8.  Find Maximum of Three Numbers in Python
  • 9.  Using Python to Read File Word by Word
  • 10.  How to Repeat a Function in Python

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