Dictionaries in Python with Examples

8/16/2025

#Dictionaries in Python with Examples

Go Back

Dictionaries in Python with Examples

Introduction

In Python, a dictionary is one of the most powerful and widely used data structures. It allows developers to store data in key-value pairs, making it highly efficient for lookups, mappings, and structured data storage.

In this guide, you’ll learn:

  • What is a dictionary in Python?

  • How to create and use dictionaries.

  • Common dictionary operations with examples.

  • Best practices for using dictionaries effectively.


#Dictionaries in Python with Examples

What is a Dictionary in Python?

A dictionary in Python is an unordered, mutable collection where data is stored as key-value pairs. Each key in a dictionary must be unique and immutable (like strings, numbers, or tuples), while values can be of any data type.

Syntax:

dictionary_name = {key: value, key: value}

Creating a Dictionary in Python

# Creating a dictionary
student = {
    "name": "Alice",
    "age": 22,
    "course": "Computer Science"
}

print(student)
# Output: {'name': 'Alice', 'age': 22, 'course': 'Computer Science'}

✅ Keys are "name", "age", "course", and their values are "Alice", 22, and "Computer Science".


Accessing Dictionary Values

# Accessing values using keys
print(student["name"])   # Output: Alice
print(student.get("age"))  # Output: 22

Modifying a Dictionary

# Adding a new key-value pair
student["grade"] = "A"

# Updating a value
student["age"] = 23

print(student)
# Output: {'name': 'Alice', 'age': 23, 'course': 'Computer Science', 'grade': 'A'}

Removing Items from a Dictionary

# Removing a key-value pair
student.pop("course")
print(student)  # Output: {'name': 'Alice', 'age': 23, 'grade': 'A'}

# Removing the last inserted item
student.popitem()
print(student)  # Output: {'name': 'Alice', 'age': 23}

# Deleting a key
del student["age"]
print(student)  # Output: {'name': 'Alice'}

Looping Through a Dictionary

person = {"name": "Bob", "age": 30, "city": "New York"}

# Loop through keys
for key in person:
    print(key, ":", person[key])

# Loop through key-value pairs
for key, value in person.items():
    print(key, "=>", value)

Output:

name : Bob  
age : 30  
city : New York  

Useful Dictionary Methods in Python

MethodDescriptionExample Output
dict.keys()Returns all keys['name', 'age']
dict.values()Returns all values['Alice', 22]
dict.items()Returns key-value pairs[('name', 'Alice'), ('age', 22)]
dict.update()Updates dictionary with another dictmerged values
dict.clear()Removes all items{}

Best Practices for Dictionaries

  • ✅ Use dictionaries when data is structured as key-value mappings.

  • ✅ Choose keys that are unique and descriptive.

  • ✅ Use .get() instead of direct access to avoid KeyError.

  • ✅ Use dictionary comprehensions for concise transformations.

Example of Dictionary Comprehension:

squares = {x: x*x for x in range(1, 6)}
print(squares)  # Output: {1: 1, 2: 4, 3: 9, 4: 16, 5: 25}

Final Thoughts

Dictionaries in Python provide a fast, flexible, and easy-to-use data structure for key-value mappings. Whether you’re handling JSON data, storing configuration details, or building applications, mastering dictionaries will make your Python coding more efficient.


✅ SEO Meta Description

"Learn Python dictionaries with examples. Understand key-value pairs, syntax, operations, methods, and best practices for using dictionaries in Python programming."

✅ SEO Keywords

dictionaries in python, python dictionary example, python key-value pairs, python dictionary methods, dictionary comprehension python, python data structures, python dictionary tutorial

Table of content