How to Create a Dictionary in Python
Python provides several built-in data structures for storing and managing information, and a dictionary is one of the most useful among them. A dictionary stores data in the form of key-value pairs, making it easy to organize and access information using a unique key.
For example, you can use a dictionary to store a student’s name, age, marks, and course. Instead of accessing values through numerical indexes like a list, a dictionary allows you to access information using meaningful keys.
Python dictionaries are flexible and can contain different types of values, including strings, numbers, lists, and even other dictionaries. This makes them useful for many programming tasks and real-world applications.
In this tutorial, you will learn how to create a dictionary in Python, how to add and remove elements, and some commonly used dictionary methods with simple examples.
Table of Contents

Complete Advance AI Topics: Click Here
SQL Tutorial: Click Here
What Is a Dictionary in Python?
A dictionary stores data as key-value pairs. Each item contains a key and its corresponding value.
- Key: A unique identifier used to access a value. Keys must be immutable, such as strings, numbers, or tuples.
- Value: The information associated with the key. Values can be strings, numbers, lists, dictionaries, or other Python objects.
For example:
student = {
"name": "Akshay",
"age": 21,
"course": "BCA"
}
print(student)
Output:
{'name': 'Akshay', 'age': 21, 'course': 'BCA'}
Here, name, age, and course serve as keys, while their corresponding information is stored as values.
How to Create a Dictionary in Python
Python provides several ways to create a dictionary. The two commonly used approaches are using curly braces {} and the dict() constructor.
1. Creating a Dictionary Using Curly Braces
The simplest way to create a dictionary is by placing key-value pairs inside curly braces. Each pair is separated by a comma, while the key and value are separated using a colon.
Example
dictionary = {
1: "Python",
2: "Dictionaries",
3: "Tutorial"
}
print("Dictionary created using curly braces:")
print(dictionary)
Output
Dictionary created using curly braces:
{1: 'Python', 2: 'Dictionaries', 3: 'Tutorial'}
Python dictionaries can also contain keys of different data types.
dictionary = {
"Website": "LearnPython",
42: [7, 8, 9, "Key-Value"]
}
print(dictionary)
In this example, the dictionary contains a string key and an integer key. The value associated with the integer key is a list.
2. Creating a Dictionary Using dict()
Python’s built-in dict() constructor provides another way to create dictionaries. It can create an empty dictionary or build one from existing key-value pairs.
Example
dictionary = {}
print("Empty dictionary:")
print(dictionary)
dictionary = dict({
1: "Learn",
2: "Python",
3: "Dictionaries"
})
print("Dictionary created using dict():")
print(dictionary)
Output
Empty dictionary:
{}
Dictionary created using dict():
{1: 'Learn', 2: 'Python', 3: 'Dictionaries'}
You can also create a dictionary from a list of key-value tuples.
dictionary = dict([
(1, "Key1"),
(2, "Key2"),
(3, "Key3")
])
print(dictionary)
Output:
{1: 'Key1', 2: 'Key2', 3: 'Key3'}
Adding Elements to a Dictionary
Adding new elements to a dictionary is simple. You can assign a value to a new key or use the update() method to add multiple key-value pairs.
Using Key Assignment
dictionary = {}
dictionary[1] = "Python"
dictionary[2] = "Tutorial"
print(dictionary)
Output:
{1: 'Python', 2: 'Tutorial'}
Using update()
The update() method can add one or more key-value pairs to an existing dictionary.
dictionary.update({3: "Dictionaries"})
print(dictionary)
Output:
{1: 'Python', 2: 'Tutorial', 3: 'Dictionaries'}
If the specified key already exists, update() modifies its value instead of adding a duplicate key.
Adding Lists and Nested Dictionaries
A dictionary value can be a list or another dictionary. This makes dictionaries useful for organizing and storing structured information.
Adding a List as a Value
dictionary = {
1: "Python",
2: "Tutorial"
}
dictionary["ListValues"] = [1, 2, 3]
print(dictionary)
Output:
{1: 'Python', 2: 'Tutorial', 'ListValues': [1, 2, 3]}
Creating a Nested Dictionary
dictionary[4] = {
"NestedKey": {
"SubKey1": "Value1",
"SubKey2": "Value2"
}
}
print(dictionary)
A dictionary contained within another dictionary is known as a nested dictionary. It is useful when information has multiple levels.
Accessing Dictionary Elements
You can retrieve a dictionary value by specifying its key inside square brackets.
student = {
"name": "Akshay",
"age": 21
}
print(student["name"])
print(student["age"])
Output:
Akshay
21
You can also use the get() method to safely retrieve a value.
print(student.get("name"))
print(student.get("marks", "Not available"))
If the specified key is not found, get() can return a default value instead of raising an error.
Removing Elements from a Dictionary
Python provides several ways to remove dictionary elements. The commonly used options are pop(), popitem(), clear(), and the del keyword.
Using pop()
The pop() method removes a specified key from a dictionary and returns its corresponding value.
dictionary = {
1: "Python",
2: "Dictionaries",
3: "Tutorial"
}
dictionary.pop(2)
print(dictionary)
Output:
{1: 'Python', 3: 'Tutorial'}
Using popitem()
The popitem() method removes and returns the last inserted key-value pair.
dictionary.popitem()
print(dictionary)
Using clear()
The clear() method removes all items from a dictionary but keeps the dictionary itself available.
dictionary.clear()
print(dictionary)
Output:
{}
Using del
The del keyword can remove a particular key or delete the complete dictionary.
dictionary = {
"name": "Akshay",
"age": 21
}
del dictionary["age"]
print(dictionary)
Output:
{'name': 'Akshay'}
Common Dictionary Methods in Python
| Method | Description |
|---|---|
clear() | Removes all elements from the dictionary. |
copy() | Creates a shallow copy of the dictionary. |
fromkeys() | Creates a dictionary using specified keys and a common value. |
get(key) | Returns the value associated with a key. |
items() | Returns the dictionary’s key-value pairs. |
keys() | Returns the dictionary keys. |
values() | Returns the dictionary values. |
pop(key) | Removes a specified key and returns its value. |
popitem() | Removes the last inserted key-value pair. |
update() | Adds or updates key-value pairs. |
Practical Example of a Python Dictionary
A dictionary provides a simple and organized way to store information about a student.
student = {
"name": "Rahul",
"age": 21,
"course": "BCA",
"marks": 85
}
print("Name:", student["name"])
print("Age:", student["age"])
print("Course:", student["course"])
print("Marks:", student["marks"])
This example shows how related information can be stored using meaningful keys and accessed whenever required.
Conclusion
A dictionary is a useful Python data structure for storing and organizing information as key-value pairs. You can create a dictionary using curly braces or the dict() constructor. New elements can be added using key assignment or update(), while existing elements can be removed using methods such as pop(), popitem(), clear(), or the del keyword.
Dictionaries can also contain lists and nested dictionaries, making them suitable for organizing structured data. Once you understand how dictionaries work, you can use them effectively in Python projects, data processing, and many other programming tasks.
Frequently Asked Questions (FAQ)
1. How do you create a dictionary in Python?
You can create a dictionary using curly braces:
student = {
"name": "Akshay",
"age": 21
}
2. How do you create an empty dictionary?
Use empty curly braces:
dictionary = {}
3. How do you add elements to a dictionary in Python?
You can assign a value to a new key:
dictionary["name"] = "Akshay"
You can also use the update() method.
4. Can a dictionary contain a list?
Yes. A list can be stored as a dictionary value:
student = {
"subjects": ["Python", "DBMS", "Java"]
}
5. What is a nested dictionary?
A nested dictionary is a dictionary stored inside another dictionary. It is useful for representing data with multiple levels.
6. How do I remove an element from a dictionary?
You can use pop(), popitem(), clear(), or the del keyword depending on what you want to remove.
7. How do I access a dictionary value?
Use its key with square brackets or the get() method:
student["name"]
student.get("name")
Related Keywords
dictionary in Python example, how to create a dictionary in Python, Python create dictionary from list, how to create a dictionary in Python using for loop, how to create a dictionary in Python with user input, how to add elements in dictionary in Python, dictionary methods in Python, how to create a dictionary in Python full explanation, how to create a dictionary in Python with example, Python dictionary tutorial, Python dictionary methods, nested dictionary in Python, list of dictionaries in Python, Python compiler, set in Python.
Download New Real Time Projects: Click Here