✍️Introduction
A dictionary in Python is used to store data in key–value pairs.
It is very useful when you want to store related information like a person’s details, product info, or student records.
Dictionaries are written using curly braces
{}.-
Each key in a dictionary must be unique.
-
Dictionaries are mutable, which means we can change their values.
-
They allow fast data access using keys instead of indexes.
What is a Dictionary in Python?
A dictionary is:
Dictionaries are ordered (from Python 3.7+).
-
They are changeable (mutable), so values can be updated.
-
Keys must be unique, but values can be duplicated.
Example:
student = { # Create a dictionary
"name": "Rahul", # Key: name, Value: Rahul
"age": 20, # Key: age, Value: 20
"course": "Python" # Key: course, Value: Python
}
print(student) # Print the dictionary
Dictionary Syntax :
Dictionary syntax defines how to create a dictionary using key–value pairs inside curly brackets {}.
A dictionary is created using
{ }.-
Each element consists of a key and a value separated by a colon
:. -
Multiple key–value pairs are separated by commas
,.
-
Keys must be unique and usually written as strings or numbers.
Syntax :
Access Dictionary Values :
Dictionary values can be accessed using their keys.
We use the key inside square brackets
[ ]to access a value.-
The
get()method is another way to access values safely. -
If the key does not exist,
get()returns None instead of an error.
-
Using
[]gives an error if the key is missing.
Example:Remove Items from Dictionary :
Items can be removed from a dictionary using different methods like pop(), popitem(), del, and clear().
Dictionaries allow deleting specific or all items.
-
pop()removes an item using its key. -
popitem()removes the last inserted item. -
delremoves a specific key–value pair. clear()removes all items from the dictionary.
Dictionary Length :
The len() function is used to count the number of key–value pairs in a dictionary.
It returns the total number of items in the dictionary.
-
Each key–value pair is counted as one item.
-
Useful for checking how much data is stored.
Output:name
age
city
Rutuja
20
Pune
name : Rutuja
age : 20
city : Pune
"Code executed successfully" Note:
-
student→ keys -
student.values()→ values -
student.items()→ key + value pairs
Dictionary Methods (Common) :
Methods make dictionaries easier to use and manage.
-
They help in retrieving keys, values, and items quickly.
-
Some methods modify the dictionary, while others only read data.
-
These methods are commonly used in real-world programs and projects.
| Method | Description |
|---|---|
| keys() | Returns keys |
| values() | Returns values |
| items() | Returns key–value pairs |
| get() | Access value |
| update() | Update dictionary |
| pop() | Remove item |
| clear() | Remove all |
Check if Key Exists :
We can check whether a key exists in a dictionary using the in keyword.
The
inkeyword searches for a key in the dictionary.-
It returns True if the key exists, otherwise False.
-
This helps avoid errors when accessing dictionary values.
-
It is commonly used before using
student["key"].
Nested Dictionary :
A nested dictionary is a dictionary inside another dictionary.
It is used to store multiple records in a structured way.
-
Each key can contain another dictionary as its value.
-
Useful for storing student data, employee records, or product details.
-
Data can be accessed using multiple keys.
Dictionary with Different Data Types :
A dictionary can store different types of values such as strings, numbers, and booleans in a single structure.
Keys are usually strings, but values can be of any data type.
-
Dictionaries can store integers, floats, strings, lists, and booleans together.
-
This makes dictionaries very useful for storing mixed data like program settings or user information.
-
Each value is accessed using its key.
Convert Dictionary to List :
list() function along with dictionary methods like keys(), values(), or items().-
list()converts them into a list format. -
This is useful when you want to process dictionary data like a list.
-
You can also convert values and key–value pairs into lists.
Note:
You can also use:
-
list(student.values())→ List of values -
list(student.items())→ List of key–value pairs
Difference Between List, Tuple, Set, Dictionary :
List, Tuple, Set, and Dictionary are the main data structures in Python used to store collections of data.
| Feature | List | Tuple | Set | Dictionary |
|---|---|---|---|---|
| Ordered | Yes | Yes | No | Yes |
| Changeable | Yes | No | Yes | Yes |
| Duplicates | Yes | Yes | No | Keys ❌ |
| Indexing | Yes | Yes | No | Keys |
Note:
-
List → General purpose collection
-
Tuple → Fixed data
-
Set → Unique values
-
Dictionary → Key–value data storage
Real-Life Example :
A dictionary can be used to store real-life data such as product details in key–value pairs.
❌ Common Mistakes :
-
Using duplicate keys
-
Accessing non-existing key without
get() -
Confusing list indexing with dictionary keys
Conclusion :
Python dictionaries are powerful and widely used in real-world applications.
They make data handling easy, readable, and efficient.
💬 Quick Question
What will be the output?
Comment your answer 👇😊
📌 Related Articles
Comments
Post a Comment