Dictionaries are one of the most powerful and widely used data structures in Python. They allow you to store data in key-value pairs, making it easy to organize, access, and manage information efficiently. Unlike lists or tuples, dictionaries use keys instead of index positions, which makes data retrieval faster and more intuitive.

๐น What is a Dictionary in Python?
A dictionary is an unordered collection of items where each item is stored as a pair of a key and a value.
โ Syntax:
my_dict = {
"name": "Rahul",
"age": 20,
"city": "Ranchi"
}
Here:
"name","age","city"are keys"Rahul",20,"Ranchi"are values
๐น Why Use Dictionaries?
- Store structured data efficiently
- Access data using meaningful keys
- Fast lookup compared to lists
- Useful for real-world data like user profiles, JSON data, etc.
๐น Accessing Values in a Dictionary
You can access values using keys.
student = {
"name": "Amit",
"marks": 85
}print(student["name"])
print(student["marks"])
Output:
Amit
85
You can also use the .get() method:
print(student.get("name"))
๐น Adding and Updating Values
Dictionaries are mutable, so you can modify them.
โ Add a new key-value pair:
student["grade"] = "A"
โ Update an existing value:
student["marks"] = 90
๐น Removing Items from a Dictionary
There are several ways to remove elements:
student = {"name": "Amit", "age": 20, "city": "Ranchi"}student.pop("age") # Removes specific key
del student["city"] # Deletes a key
student.clear() # Removes all items
๐น Iterating Through a Dictionary
You can loop through keys, values, or both.
โ Loop through keys:
for key in student:
print(key)
โ Loop through values:
for value in student.values():
print(value)
โ Loop through key-value pairs:
for key, value in student.items():
print(key, ":", value)
๐น Dictionary Methods
Some commonly used dictionary methods:
.keys()โ Returns all keys.values()โ Returns all values.items()โ Returns key-value pairs.get(key)โ Safely retrieves a value.pop(key)โ Removes a key
๐น Real-Life Use Cases
Dictionaries are widely used in real-world applications:
- Storing user profiles (name, age, email)
- Representing JSON data in APIs
- Counting frequency of words in text
- Managing configuration settings
- Database-like structures in programs
๐น Example: Word Count Program
text = "apple banana apple orange banana apple"words = text.split()
word_count = {}for word in words:
word_count[word] = word_count.get(word, 0) + 1print(word_count)
Output:
{'apple': 3, 'banana': 2, 'orange': 1}
๐น Best Practices
- Use meaningful and unique keys
- Avoid mutable types as keys (like lists)
- Use
.get()to avoid errors - Keep dictionary data structured and clean
- Use dictionaries for fast lookups
Dictionaries in Python are essential for handling structured data using key-value pairs. They provide fast access, flexibility, and ease of use when working with real-world data. By mastering dictionaries, you can efficiently store, retrieve, update, and iterate through data in your Python programs, making your code more powerful and organized.
For More Information and Updates, Connect With Us
- Nameย Sumit singh
- Phone Number: +91 9264477176
- Email ID:ย emancipationedutech@gmail.com
- Our Platforms:
- Digilearn Cloud
- Live Emancipation
- Follow Us on Social Media:
- Instagram โ Emancipation
- Facebook โย Emancipation
Stay connected and keep learning withย Python Trainingย !