Python Dictionaries

Last Updated : 15 Apr 2026

Python dictionary is used to store data in key-value pairs. In this chapter, you will learn about Python dictionaries, their features, and how to use them with examples.

What is a Dictionary in Python?

A dictionary is an unordered and mutable collection of data stored in key-value pairs. Each key is unique and is used to access its corresponding value.

Dictionaries are useful for storing related data and retrieving values using keys.

Python Dictionaries

Example

Let us take a look at a simple example of a dictionary.

Execute Now

Output:

{1: 'Learn', 2: 'Python', 3: 'from', 4: 'Tpoint', 5: 'Tech'}

Explanation:

In the above example, we have created a simple dictionary consisting of multiple 'key: value' pairs.

As we can observe, a dictionary in Python is a mapping data type where the value of one object maps to another. In order to establish the mapping between a key and a value, we have used the colon ':' symbol between the two.

Characteristics of Python Dictionary

A dictionary is a data type with the following characteristics:

  • Mutable: Dictionaries can be modified after initialization allowing us to add, remove or update 'key: value' pairs.
  • Unordered: The dictionary does not follow a particular order to store items. However, starting from Python 3.7, the feature for the dictionary to maintain the insertion order of the items was added.
  • Indexed: Unlike lists or tuples, which are indexed by position, dictionaries use keys to access values, offering faster and more readable data retrieval.
  • Unique Keys: Each key in a dictionary must be unique. If we try to assign a value to an existing key, the old value will be replaced by the new one.
  • Heterogeneous: Keys and values in a dictionary can be of any type.

Creating a Dictionary

We can create a dictionary by enclosing the sequence of 'key: value' pairs with curly braces separated by commas. As an alternate option, we can use the Python's built-in dict() function.

Example

Here is a simple example showing both ways of creating a dictionary.

Execute Now

Output:

Empty Dictionary: {}
Dictionary 1 (created using {}): {'name': 'Lucy', 'age': 19, 'city': 'New Jersey'}
Dictionary 2 (created using dict()): {'name': 'John', 'age': 21, 'city': 'Havana'}

Explanation:

The above example shows different ways to create dictionaries in Python. We have also seen how to create an empty dictionary.

Note: The dict() function can also be used to transform an existing data type into a dictionary.

Accessing Dictionary Items

We can access the value of a dictionary item by enclosing that particular key with square brackets '[]'. Another way to access dictionary items is by the use of the get() method.

Example

The following is a simple example showing the ways to access dictionary items.

Execute Now

Output:

Person's Details
Name: Sachin
Age: 18
Gender: male
Profession: student

Explanation:

Here, we have accessed the different values of the dictionary items using the square brackets and get() method.

Adding Items to a Dictionary

The dictionary is a mutable data type that allows us to add an item to it. It can be done by assigning a value to a new key.

Example

Let us take a look at a simple example showing how to add items to a Python dictionary.

Execute Now

Output:

Given Dictionary: {'name': 'Sachin', 'age': 18, 'gender': 'male', 'profession': 'student'}
Updated Dictionary: {'name': 'Sachin', 'age': 18, 'gender': 'male', 'profession': 'student', 'country': 'India'}

Explanation:

In this example, we have added a new 'key: value' pair to the dictionary using the assignment operator.

Removing Items from a Dictionary

Python offers multiple ways to remove items from a given dictionary, such as:

  • del: This keyword is used to remove an item by key.
  • pop(): This method is used to remove an item by key. It also returns the value of the removed item.
  • popitem(): This method removes and returns the last 'key: value' pair.
  • clear(): This method is used to remove all items from the dictionary.

Example

Here is an example showing the use of different methods to remove items from a Python dictionary.

Execute Now

Output:

Given Dictionary: {'name': 'Sachin', 'age': 18, 'gender': 'male', 'profession': 'student', 'country': 'India'}
Updated Dictionary (Removed 'age'): {'name': 'Sachin', 'gender': 'male', 'profession': 'student', 'country': 'India'}
Updated Dictionary (Removed 'gender'): {'name': 'Sachin', 'profession': 'student', 'country': 'India'}
Popped Value: male
Updated Dictionary (Removed last item): {'name': 'Sachin', 'profession': 'student'}
Popped Item: ('country', 'India')
Update Dictionary (Removed all items): {}

Explanation:

In this example, we are given a dictionary. We have used several methods like del keyword, pop(), popitem(), and clear() methods to remove the items from the dictionary.

Changing Dictionary Items

We can change the values of an item in the dictionary by referring to its key.

Example

Let us take a simple example to understand how to change dictionary items in Python.

Execute Now

Output:

Given Dictionary: {'name': 'Sachin', 'age': 18, 'gender': 'male', 'profession': 'student', 'country': 'India'}
Updated Dictionary: {'name': 'Sachin', 'age': 20, 'gender': 'male', 'profession': 'developer', 'country': 'India'}

Explanation:

In this example, we have used the assignment operator to change the value of existing keys in the given dictionary. As a result, the dictionary items are updated.

Iterating Through a Dictionary

Starting from Python 3.7, a dictionary is an ordered collection of items; therefore, it maintains the order of its items. We can iterate through dictionary keys using the 'for' loop.

Example

Let us take an example to demonstrate how to iterate key through a dictionary.

Execute Now

Output:

Items in Dictionary:
Name -> Sachin
Age -> 18
Gender -> Male
Profession -> Student
Country -> India

Explanation:

In the above example, we have used the 'for' loop to iterate through the keys in dictionary and accessed the value for each key.

Finding Length of a Dictionary

In order to find the length of a dictionary, we can use Python's built-in function called len(). This function will return the total number of 'key: value' pairs present in a dictionary, allowing us to determine the size of the dictionary efficiently.

Example

Let us see the following example showing the use of the len() function in determining the length of a Python dictionary.

Execute Now

Output:

Given Data: {'John': 'Sr. Software Developer', 'Irfan': 'UI/UX Designer', 'Lucy': 'Human Resource Manager', 'Peter': 'Team Lead', 'Johnson': 'Business Developer'}
Size of Data: 5

Explanation:

In the above example, we have used the len() function in order to find out how many items are in the given dictionary.

Dictionary Membership Test

We can use the 'in' or 'not in' operators in order to check whether a key exists in a dictionary.

Example

Here's a simple example that shows how to see if a specified key is part of a dictionary in Python.

Execute Now

Output:

Is 'fruit' a member of 'dict_y'?: True
Is 'beverage' a member of 'dict_y'?: False
Is 'beverage' NOT a member of 'dict_y'?: True

Explanation:

In this example, we have used the 'in' and 'not in' operators to check if the specified keys are present in the given dictionary. The 'in' operator returns the Boolean value after checking if the key exists in the dictionary, whereas the 'not in' operator returns the Boolean value after checking if the key does not exist in it.

Dictionary Methods in Python

Python provides several dictionary methods to work with dictionary data. These methods help us add, update, delete, and access elements in a dictionary.

Some commonly used dictionary methods are as follows:

Dictionary MethodDescription
get()This method returns the value associated with a specific key.
update()This method is utilized to add a new item to the dictionary or update the value of an existing key.
copy()This method is utilized to return a copy of the dictionary.
pop()This method removes the item with the given key from the dictionary.
popitem()This method is utilized to return the last inserted key and value as a tuple.
clear()This method removes all items from the dictionary.
keys()This method returns all the keys in the dictionary.
values()This method is utilized to return all the values in the dictionary.