A dictionary in Python is used to store data in the form of key–value pairs.
Each key acts as a unique identifier for its associated value.
A dictionary has the following characteristics:
Ordered (from Python 3.7 onwards)
Changeable (mutable)
Does not allow duplicate keys
Creating a Dictionary
Dictionaries are created using curly braces
{}, with each
item written as
key: value.
Example: Create and display a dictionary
Dictionary Items
Each item in a dictionary consists of a key and a value. You can access a
value by referring to its key name.
Example: Access a specific value
Ordered vs Unordered Dictionaries
From Python 3.7 and later, dictionaries maintain the order in which items
are inserted. In Python 3.6 and earlier, dictionaries do not guarantee
order.
Ordered means items stay in the same sequence.
Unordered means items do not have a fixed position and
cannot be accessed using an index.
Changeable Nature of Dictionaries
Dictionaries allow you to add, modify, or remove items after creation.
Example: Modify a value
Duplicate Keys Not Allowed
A dictionary cannot contain two items with the same key. If a duplicate key
is used, the latest value replaces the old one.
Example: Duplicate key behavior
Dictionary Length
To find the number of key–value pairs in a dictionary, use the
len()
function.
Dictionary Values and Data Types
Dictionary values can store any data type, including strings, numbers,
booleans, and lists.
Example: Multiple data types in a dictionary
Dictionary Data Type
From Python’s perspective, dictionaries belong to the
dict data
type.
Using the dict() Constructor
You can also create a dictionary using the built-in
dict()
function.
Python Collection Data Types
List – Ordered, changeable, allows duplicate values
Set – Unordered, unchangeable*, does not allow duplicates
Dictionary – Ordered**, changeable, does not allow
duplicate keys
Accessing Dictionary Items in Python
You can retrieve values from a dictionary by referring to their key names.
Python provides multiple ways to access dictionary data safely and
efficiently.
Access Values Using Square Brackets
The most common way to access a dictionary value is by using the key inside
square brackets. If the key does not exist, this method raises a KeyError.
Access Values Using the get() Method
The
get() method
also retrieves values using keys, but it is safer because it does not throw
an error if the key is missing.
Get All Dictionary Keys
The
keys() method
returns a view object containing all the keys in the dictionary.
The keys view reflects changes made to the dictionary automatically.
Get All Dictionary Values
The
values()
method returns a view of all the values stored in the dictionary.
Updating an existing value dynamically updates the view:
Adding a new key-value pair also updates the view:
Get All Dictionary Items
The
items()
method returns a view containing key–value pairs as tuples.
Modifying an existing item:
Adding a new item:
Check If a Key Exists in a Dictionary
You can use the
in keyword to
verify whether a specific key exists in a dictionary.
Changing Values in a Dictionary
In Python, dictionary values can be modified easily by referencing their key
names. Since dictionaries are mutable, you can update existing entries after
the dictionary is created.
Change a Value Using the Key
Updating Dictionary Using update()
The
update()
method allows you to change or add multiple key–value pairs at once. The
argument passed must be either another dictionary, or an iterable containing
key–value pairs.
Example: Update multiple values at once:
Key Difference: Direct assignment updates only one key at a
time, while
update() can
modify or insert multiple items in a single statement.
Adding Items to a Dictionary
Add an Item Using a New Key
Add Items Using the update() Method
Example: Add multiple items at once
Removing Items from a Dictionary
1. Removing an Item Using pop()
2. Removing the Last Inserted Item Using popitem()
3. Removing an Item Using del
Looping Through a Dictionary
You can iterate over a dictionary using a
for loop. By
default, looping through a dictionary returns its keys, but Python also
provides built-in methods to access values and key–value pairs.
1. Looping Through Keys (Default Behavior)
2. Looping Through Values Using Keys
3. Looping Through Values Using values()
4. Looping Through Keys Using keys()
5. Looping Through Keys and Values Using items()
Copying a Dictionary in Python
In Python, assigning one dictionary to another using
dict2 = dict1
does not create a new dictionary. Instead, both variables point to the same
object in memory. To create an independent copy, Python provides safe
methods.
Method 1: Using the copy() Method
Method 2: Using the dict() Constructor
Nested Dictionaries in Python
A nested dictionary is a dictionary that stores one or more dictionaries as
its values. This structure is useful when you need to represent grouped or
hierarchical data.
Example 1: Creating a Nested Dictionary Directly
Example 2: Combining Multiple Dictionaries into One
Accessing Values from a Nested Dictionary
To access data inside a nested dictionary, use multiple keys—starting from
the outer dictionary and moving inward.
Looping Through Nested Dictionaries
You can loop through a nested dictionary using the
items()
method to access both keys and values.