“Are Dictionaries Mutable Python?” If True, Why Is It True?

Are dictionaries mutable python

Are dictionaries mutable Python?” is a question that many beginners look up when they are new to Python. In this article, we will answer that question in detail with many examples to make it easy for you to understand.

The answer to the question: “Are dictionaries mutable Python?”

To answer the question, first, let’s find out what a Dictionary in Python is.

What is Dictionary in Python?

Dictionary is an unordered collection. Its elements are a pair consisting of a key and a corresponding value. The keys in the Dictionary are immutable, but the values ​​are mutable and ​​can be changed to any data type.

Based on the above definition, we can quickly determine that dictionaries in Python are mutable because we can change their value.

Below are some ways to prove Dictionary is mutable.

Adding an element to the Dictionary

If an object is immutable, then we cannot add elements to that object. So let’s try adding an element to the Dictionary.

learningWebsites = {
  'language': 'duolingo.com',
  'editing': 'learning.adobe.com',
  'skill': 'skillshare.com'
}

# Add an element to Dictionary
learningWebsites['programming'] = 'learnshareit.com'

print(learningWebsites)

Output:

{
  'language': 'duolingo.com',
  'editing': 'learning.adobe.com',
  'skill': 'skillshare.com',
  'programming': 'learnshareit.com'
}

As you can see, we can add an element to the Dictionary without any errors. Therefore, dictionaries are exactly mutable.

Updating the value of the Dictionary

You can also update an object to check if it is mutable. The object is mutable if there are no errors when updating the value.

learningWebsites = {
  'language': 'duolingo.com',
  'editing': 'learning.adobe.com',
  'skill': 'skillshare.com',
  'programming': 'learnshareit.com'
}

# Update the value of the key 'skill'
learningWebsites['skill'] = 'udemy.com'

print(learningWebsites)

Output:

{
  'language': 'duolingo.com',
  'editing': 'learning.adobe.com', 
  'skill': 'udemy.com',
  'programming': 'learnshareit.com'
}

Deleting an element from the Dictionary

Deleting an element from a Dictionary is also a way to check if a Dictionary is a mutable object. 

When we delete an element in an immutable object, our application will crash, and Python will throw an error. If we delete an element in the Dictionary and our application still runs normally, it means that dictionaries are mutable.

learningWebsites = {
  'language': 'duolingo.com',
  'editing': 'learning.adobe.com',
  'skill': 'skillshare.com',
  'programming': 'learnshareit.com'
}

# Delete the website to learn to edit
del learningWebsites['editing']

print(learningWebsites)

Output:

{
  'language': 'duolingo.com',
  'skill': 'skillshare.com',
  'programming': 'learnshareit.com'
}

Summary

That’s enough for you to answer the question: “Are dictionaries mutable Python?”. The answer is yes; dictionaries in Python are mutable objects. You can use one of three methods. It is not necessary to use all three. Besides Dictionary, several other data types are also mutable. Please wait for our next articles to know what data types they are.

Have a nice day!

Maybe you are interested:

Leave a Reply

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