A dictionary in Python is an unordered collection of key-value pairs. It is defined using curly braces {} and provides an efficient way to store and retrieve data using unique keys.
Creating a Dictionary
Dictionaries are created using curly braces {} or the dict() constructor.
# Using curly braces student = { "name": "Alice", "age": 25, "city": "New York" } # Using the dict() constructor person = dict(name="John", age=30, city="San Francisco") print(student) # {'name': 'Alice', 'age': 25, 'city': 'New York'} print(person) # {'name': 'John', 'age': 30, 'city': 'San Francisco'}
Accessing Elements in a Dictionary
You can access dictionary values using keys.
student = { "name": "Alice", "age": 25, "city": "New York" } # Using square brackets print(student["name"]) # Alice # Using the get() method (prevents KeyError) print(student.get("age")) # 25 print(student.get("gender", "Not found")) # Not found
Updating a Dictionary
You can modify dictionary values by accessing them through keys.
student = { "name": "Alice", "age": 25, "city": "New York" } student["age"] = 26 student["city"] = "Boston" print(student) # {'name': 'Alice', 'age': 26, 'city': 'Boston'}
- Adding New Elements
You can add new key-value pairs.
student = { "name": "Alice", "age": 25, "city": "New York" } student["course"] = "Computer Science" print(student) # {'name': 'Alice', 'age': 25, 'city': 'New York', 'course': 'Computer Science'}
- Removing Elements from a Dictionary
You can remove dictionary elements using del, pop(), popitem(), and clear().
student = { "name": "Alice", "age": 25, "city": "New York", "gender": "Female", } # Using del del student["age"] # Using pop() course = student.pop("gender") # Removes and returns value # Using popitem() (removes last inserted item) last_item = student.popitem() # Using clear() (removes all items) student.clear() print(student) # {}
Iterating Over a Dictionary
You can iterate over a dictionary using loops.
student = { "name": "Alice", "age": 25, "city": "New York", "gender": "Female", } # Iterating Over Keys for key in student: print(key, student[key]) # Iterating Over Values for value in student.values(): print(value) # Iterating Over Key-Value Pairs for key, value in student.items(): print(key, value)
Checking Key Existence
student = { "name": "Alice", "age": 25, "city": "New York", "gender": "Female", } if "name" in student: print(f"Name: {student["name"]}") # Name: Alice
Dictionary Comprehension
You can create dictionaries using dictionary comprehension.
squares = {x: x*x for x in range(1, 6)} print(squares) # {1: 1, 2: 4, 3: 9, 4: 16, 5: 25}
Nesting in Dictionaries
Dictionaries can store lists or other dictionaries.
# Nesting Lists in Dictionaries students = { "Alice": ["Math", "Science"], "Bob": ["History", "Geography"] } print(students["Alice"]) # ['Math', 'Science'] # Nesting Dictionaries in Dictionaries class_data = { "Alice": {"age": 25, "city": "New York"}, "Bob": {"age": 22, "city": "Chicago"} } print(class_data["Alice"]["city"]) # New York