Is Dictionary Mutable in Python?

What is a Python dictionary?

Dictionaries are mutable data structures in Python that are similar to lists, sets, and tuples but are indexed by keys instead of numbers. They can be thought of as associative arrays, consisting of keys with associated values.

A Dictionary in Python is a list of key-value pairs that are separated by commas and put inside curly brackets {}. When the key is known, it is easiest to get the value from a dictionary.

What are Keys?

Keys are immutable data types that can be either strings or numbers. They are unique within a Dictionary and cannot be duplicated. If used more than once, subsequent entries will overwrite the previous value.

Dictionary is Mutable or Immutable in Python?

Since the dictionary is mutable, entries can be added, removed, or changed at any time. Note, though, that we can’t have two entries with the same key because each entry is accessed by its key.

You must comprehend that Python stores all of its data as objects. The mutability of an object is determined by its type. Other objects, such as integers, floats, strings, and tuples, cannot be modified.

Difference Between Lists and Dictionaries

There are two types of data structures in Python – lists, and dictionaries. Lists are index-based, like arrays in C++, while dictionaries are key-value based.

A list can store a series of objects in a certain order so that you can index them into the list or go through it in a loop. Also, List is a mutable type, which means that lists can be changed after they have been created.

Python’s dictionary is an implementation of a hash table and a key-value store. It is not in any particular order, and it requires that the keys be hash table. Additionally, lists are created using square brackets while dictionaries are created using curly brackets.

Creating a Dictionary

In Python, you can create a dictionary by putting a list of items inside curly braces and putting a comma (,) between each one. Dictionary holds pairs of values, where one is the Key and the other is the value of that. Values in a dictionary can be any type of data and can be duplicated, but keys can’t be duplicated and must be immutable.

Note: Dictionary keys are case-sensitive, which means that two keys with the same name but different capitalizations will be treated differently. And the values can be integers, floats, strings, lists, etc.

Syntax

As a reminder, here is the syntax of the Dictionary.

dict_1 = {
  "key1":"value1",
  "key2":"value2"
}

Example of Creating a Dictionary

Below is an example of creating a dictionary in different key types.

# with Integer Keys
Dict = {1: 'IT', 2: 'Source', 3: 'Code'}
print("Using Integer Keys to Make a Dictionary:")
print(Dict)

# with Mixed Keys
Dict = {'Hello': 'World', 2: [1, 2, 3, 4, 5]}
print("\nUsing Mixed Keys to Make a Dictionary:")
print(Dict)

Output:

Using Integer Keys to Make a Dictionary:
{1: 'IT', 2: 'Source', 3: 'Code'}

Using Mixed Keys to Make a Dictionary:
{'Hello': 'World', 2: [1, 2, 3, 4, 5]}

If you specify a key more than once when creating a dictionary, only the last value will be used.

Example:

Dict = {1: 'IT', 2: 'Source', 3: 'Code'}

Dict[1] = "Information Technology"
print(Dict)

Output:

{1: 'Information Technology', 2: 'Source', 3: 'Code'}

To test your Python code from this lesson, use a code editor like PyCharm. You can also test your code online using our free Python Compiler.

How to access dictionary values in Python

The index cannot be used to access dictionary values.

Dict = {
    "Name": "Prince",
    "Number": 23,
    "Language": ["PHP", "Python", "Java"]
}

print(Dict[1])

If you try to do this, you’ll get an error message like this:

Traceback (most recent call last):
  File "/home/test.py", line 7, in <module>
    print(Dict[1])
KeyError: 1

Summary

This tutorial has discussed Python dictionaries, including what they are, what keys are, and whether or not they can change. It has also talked about how to make a dictionary and how to access its values.

If you missed our previous lessons, check out our list of Python tutorial for absolute beginners anytime. You can also check our Best Machine Learning Projects with Source Code if you’re interested in machine learning.

The next post will be about Python’s built-in functions. You’ll learn about their syntax and parameters, and see some examples.


Leave a Comment