What is a Dictionary?
- A Dictionary is a Python data structure used to store data in key-value pairs.
student = {
"name": "Punitha",
"age": 22,
"course": "Data Analytics"
}
print(student)
Output
{'name': 'Punitha', 'age': 22, 'course': 'Data Analytics'}
Why Do We Use Dictionary?
- Student information
- Employee information
- Product details
- Customer details
- JSON data
- API data
- Configuration data
Creating a Dictionary
- We normally create a Dictionary using {}.
student = {
"name": "Punitha",
"age": 22,
"course": "Python"
}
print(student)
Output
{'name': 'Punitha', 'age': 22, 'course': 'Python'}
Empty Dictionary
- We can create an empty Dictionary using {}.
student = {}
student = dict()
print(student)
Output
{}
Accessing Dictionary Values
- We can access a value using its key.
student = {
"name": "Punitha",
"age": 22,
"course": "Python"
}
print(student["name"])
print(student["age"])
print(student["course"])
Output:
Punitha 22 Python
Using get()
- We can also use get() to access a value.
student = {
"name": "Punitha",
"age": 22
}
print(student.get("name"))
Output:
Punitha
Adding New Items
- We can add a new key-value pair.
student = {
"name": "Punitha",
"age": 22
}
student["city"] = "Chennai"
print(student)
Output:
{'name': 'Punitha', 'age': 22, 'city': 'Chennai'}
Updating Existing Values
- If the key already exists, assigning a new value will update it.
student = {
"name": "Punitha",
"age": 22
}
student["age"] = 23
print(student)
Output
{'name': 'Punitha', 'age': 23}
Removing Items
- There are different ways to remove dictionary items.
pop()
student = {
"name": "Punitha",
"age": 22,
"city": "Chennai"
}
student.pop("age")
print(student)
Output:
{'name': 'Punitha', 'city': 'Chennai'}
- pop() removes the specified key-value pair.
popitem()
- popitem() removes the last inserted key-value pair.
student = {
"name": "Punitha",
"age": 22,
"city": "Chennai"
}
student.popitem()
print(student)
Output:
{'name': 'Punitha', 'age': 22}
del
- We can use del to remove a specific item.
student = {
"name": "Punitha",
"age": 22,
"city": "Chennai"
}
del student["age"]
print(student)
Output:
{'name': 'Punitha', 'city': 'Chennai'}
clear()
- clear() removes all items.
student = {
"name": "Punitha",
"age": 22
}
student.clear()
print(student)
Output:
{}
Dictionary Length
- We can use len() to find the number of key-value pairs.
student = {
"name": "Punitha",
"age": 22,
"city": "Chennai"
}
print(len(student))
Output:
3
Checking Keys
- We can use in to check whether a key exists.
student = {
"name": "Punitha",
"age": 22
}
print("name" in student)
print("city" in student)
Output:
True
False
Important Dictionary Methods
Method Purpose
keys() Returns all keys
values() Returns all values
items() Returns key-value pairs
get() Gets a value using a key
update() Adds or updates items
pop() Removes a specified item
popitem() Removes the last item
clear() Removes all items
copy() Creates a copy
Nested Dictionary
- A dictionary can contain another dictionary.
students = {
"student1": {
"name": "Punitha",
"age": 22
},
"student2": {
"name": "Anu",
"age": 21
}
}
print(students)
print(students["student1"]["name"])
Output:
Punitha
Dictionary with Different Data Types
- Dictionary values can have different data types.
student = {
"name": "Punitha",
"age": 22,
"marks": 85.5,
"passed": True
}
print(student)
Dictionary Comprehension
- Dictionary comprehension provides a short way to create a dictionary.
numbers = [1, 2, 3, 4, 5]
squares = {num: num ** 2 for num in numbers}
print(squares)
Output:
{1: 1, 2: 4, 3: 9, 4: 16, 5: 25}
Converting List to Dictionary
- We can use dict() with pairs.
data = [
("name", "Punitha"),
("age", 22),
("city", "Chennai")
]
student = dict(data)
print(student)
Output:
{'name': 'Punitha', 'age': 22, 'city': 'Chennai'}
Dictionary vs List
List Dictionary
Stores values Stores key-value pairs
Uses [] Uses {}
Access using index Access using key
Example students[0] Example student["name"]
Ordered Ordered by insertion in modern Python
Duplicates allowed Keys must be unique
Dictionary vs Set
Dictionary Set
Stores key-value pairs Stores values
{key: value} {value}
Access using key No indexing
Keys must be unique Values must be unique
Used for structured data Used for unique data
Top comments (0)