• 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 / Are Dictionaries Mutable in Python? Yes, Dictionaries are Mutable

Are Dictionaries Mutable in Python? Yes, Dictionaries are Mutable

July 8, 2022 Leave a Comment

In Python, there are mutable and inmutable data types. Mutable data types can be changed after it has been created. Inmutable data types cannot be changed after they have been created.

Are dictionaries mutable in Python?

Yes, dictionaries are mutable in Python.

With dictionaries, we can add and remove items easily, and also change existing items.

For example, let’s say we create the following dictionary.

d = {"a": 1, "b": 2, "c": 3}

We can easily make changes to this dictionary to prove that it is mutable.

For example, to add a key to a dictionary, we can do the following.

d = {"a": 1, "b": 2, "c": 3}

d["d"] = 4

print(d)

#Output:
{'a': 1, 'b': 2, 'c': 3, 'd': 4}

If you want to delete a key from a dictionary, you can use the del keyword.

d = {"a": 1, "b": 2, "c": 3}

del d["b"]

print(d)

#Output:
{'a': 1, 'c': 3}

To replace a value in a dictionary, you can access the key and change the value.

d = {"a": 1, "b": 2, "c": 3}

d["a"] = 0 

print(d)

#Output:
{'a': 0, 'b':2, 'c': 3}

All of these operations show that dictionaries are mutable in Python.

Other Examples of Mutable Data Types in Python

Some other data types which are mutable in Python include lists, sets and user-defined classes.

For example, with lists, we can add items to a list, remove items from lists, and easily change the items in a list.

One example of a data type which is inmutable is the tuple data type.

Tuples are inmutable because, after you’ve created it, you cannot add or remove items from the tuple or change the elements of a tuple.

Basically, if an object can be change over time, then it is mutable. If it cannot change, then it is inmutable.

Hopefully this article has been useful for you to learn that dictionaries are mutable and about mutable data types in Python.

Other Articles You'll Also Like:

  • 1.  Writing Multiple Lines Lambda Expression in Python
  • 2.  math.radians() python – How to Convert Degrees to Radians in Python
  • 3.  Python asinh – Find Hyperbolic Arcsine of Number Using math.asinh()
  • 4.  Python cube root – Find Cube Root of Number With math.pow() Function
  • 5.  pandas tail – Return Last n Rows from DataFrame
  • 6.  Using Python to Append Character to String Variable
  • 7.  Python ljust Function – Left Justify String Variable
  • 8.  Calculate Distance Between Two Points in Python
  • 9.  Format Numbers as Dollars in Python with format()
  • 10.  Get Python Dictionary Values as 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