Python Dictionary Explained for Beginners (With Simple Examples)

Python Dictionary Explained for Beginners (With Simple Examples)

Python Dictionary Explained for Beginners (With Simple Examples)

Introduction

In the previous post, we learned about Python Sets. Now let’s learn one of the most powerful and widely used data structures in Python — the Dictionary.

Dictionaries are used to store data in key-value pairs.

---

What is a Dictionary in Python?

A dictionary is a collection of data that:

  • Stores data in key : value pairs
  • Is unordered
  • Is mutable (can be changed)
  • Does not allow duplicate keys
Each key in a dictionary must be unique.
---

How to Create a Dictionary

Dictionaries are created using curly brackets { } with key-value pairs.

student = {
  "name": "Ayush",
  "age": 22,
  "course": "Python"
}

print(student)
---

Accessing Dictionary Values

You can access dictionary values using keys.

student = {
  "name": "Ayush",
  "age": 22
}

print(student["name"])
print(student["age"])
---

Using get() Method

The get() method is a safer way to access values.

student = {
  "name": "Ayush",
  "age": 22
}

print(student.get("name"))
print(student.get("marks"))
If a key does not exist, get() returns None instead of an error.
---

Adding New Items to a Dictionary

You can add new key-value pairs easily.

student = {
  "name": "Ayush",
  "age": 22
}

student["marks"] = 85
print(student)
---

Updating Dictionary Values

You can change existing values using keys.

student = {
  "name": "Ayush",
  "age": 22
}

student["age"] = 23
print(student)
---

Removing Items from a Dictionary

Use pop() or del to remove items.

student = {
  "name": "Ayush",
  "age": 22
}

student.pop("age")
print(student)
---

Looping Through a Dictionary

You can loop through keys and values using a for loop.

student = {
  "name": "Ayush",
  "age": 22,
  "course": "Python"
}

for key, value in student.items():
    print(key, ":", value)
---

Dictionary Length

Use len() to find the number of key-value pairs.

student = {"name": "Ayush", "age": 22}
print(len(student))
---

Difference Between List, Tuple, Set, and Dictionary

  • List → Ordered, Mutable, Allows duplicates
  • Tuple → Ordered, Immutable, Allows duplicates
  • Set → Unordered, Mutable, No duplicates
  • Dictionary → Key-Value pairs, Mutable, No duplicate keys
---

When Should You Use Dictionaries?

  • To store related data
  • When data has labels (keys)
  • For fast data lookup
---

What You Learned in This Post

  • What is a dictionary
  • Creating and accessing dictionaries
  • Adding, updating, and removing items
  • Looping through dictionaries
---

What’s Next?

Now that you understand all major data structures, it’s time to learn control keywords.

Next Post: Python break, continue, and pass Explained for Beginners

---

👋 About the Author

Ayush Gupta
MSc AI/ML Student | Machine Learning & Python Enthusiast

📧 Email: aygupta9898@gmail.com

Comments

Popular posts from this blog

What is Programming? Simple Explanation for Beginners

What is Python? Why Learn Python for Beginners

How to Install Python on Windows and Mac (Step-by-Step Guide)