Python Intermediate - Dictionaries and Sets
Working with dictionaries and sets in Python
Dictionaries and sets are powerful data structures in Python. Dictionaries store key-value pairs, allowing for efficient data retrieval based on keys. Sets store unique elements and are useful for performing mathematical set operations. This tutorial explores how to work with dictionaries and sets in Python.
Key Points:
- Dictionaries store key-value pairs for efficient data retrieval.
- Sets store unique elements and support set operations like union, intersection, and difference.
- Both dictionaries and sets provide efficient membership tests and support various methods for manipulation.
Dictionaries
Dictionaries in Python are mutable collections that store items in key-value pairs. They are defined using curly braces:
# Example of a dictionary
my_dict = {
"name": "Alice",
"age": 25,
"city": "Wonderland"
}
print(my_dict)
# Accessing values
print(my_dict["name"]) # Output: Alice
print(my_dict.get("age")) # Output: 25
# Adding and modifying values
my_dict["age"] = 26
my_dict["country"] = "Wonderland"
print(my_dict)
In this example, my_dict
is a dictionary containing key-value pairs. Values are accessed using keys, and new key-value pairs can be added or modified.
Dictionary Methods
Dictionaries provide various methods for manipulation, such as adding, removing, and iterating over key-value pairs:
# Dictionary methods
# Removing items
my_dict = {
"name": "Alice",
"age": 25,
"city": "Wonderland"
}
del my_dict["age"]
print(my_dict) # Output: {'name': 'Alice', 'city': 'Wonderland'}
# Iterating over keys and values
for key, value in my_dict.items():
print(f"{key}: {value}")
# Checking for keys
print("name" in my_dict) # Output: True
print("age" in my_dict) # Output: False
# Clearing the dictionary
my_dict.clear()
print(my_dict) # Output: {}
In this example, various dictionary methods are demonstrated, including removing items, iterating over key-value pairs, checking for keys, and clearing the dictionary.
Sets
Sets in Python are mutable collections that store unique elements. They are defined using curly braces or the set()
function:
# Example of a set
my_set = {1, 2, 3, 4, 5}
print(my_set)
# Sets automatically remove duplicates
my_set = {1, 2, 2, 3, 4, 4, 5}
print(my_set) # Output: {1, 2, 3, 4, 5}
# Adding and removing elements
my_set.add(6)
print(my_set) # Output: {1, 2, 3, 4, 5, 6}
my_set.remove(3)
print(my_set) # Output: {1, 2, 4, 5, 6}
In this example, my_set
is a set containing unique elements. Elements can be added and removed using the add()
and remove()
methods.
Set Operations
Sets support various mathematical set operations, such as union, intersection, and difference:
# Set operations
set1 = {1, 2, 3, 4}
set2 = {3, 4, 5, 6}
# Union
print(set1 | set2) # Output: {1, 2, 3, 4, 5, 6}
# Intersection
print(set1 & set2) # Output: {3, 4}
# Difference
print(set1 - set2) # Output: {1, 2}
# Symmetric difference
print(set1 ^ set2) # Output: {1, 2, 5, 6}
In this example, various set operations are demonstrated, including union, intersection, difference, and symmetric difference.
Set Methods
Sets provide various methods for manipulation and membership tests:
# Set methods
my_set = {1, 2, 3, 4, 5}
# Checking for membership
print(3 in my_set) # Output: True
print(6 in my_set) # Output: False
# Clearing the set
my_set.clear()
print(my_set) # Output: set()
In this example, various set methods are demonstrated, including checking for membership and clearing the set.
Summary
In this tutorial, you learned about dictionaries and sets in Python. Dictionaries are mutable collections that store key-value pairs for efficient data retrieval, while sets are mutable collections that store unique elements and support mathematical set operations. You explored how to create, access, and modify dictionaries and sets, and you learned about common methods and operations that apply to both. Understanding dictionaries and sets is essential for effective data manipulation and retrieval in Python.