Dictionaries and sets are versatile collections in Python that are used to store and manage data efficiently. Dictionaries store **key-value pairs**, acting like a real-world dictionary where you look up a word (key) to find its definition (value). Sets, on the other hand, act like mathematical sets: they are unordered collections of **unique** elements.
Python dictionaries are incredibly fast because they are implemented as **Hash Tables**. Unlike a list, where Python must scan elements one by one to find an item (Linear Time, or $O(n)$), a dictionary uses a "Hash Function" to calculate exactly where a value is stored in memory.
This allows for **Constant Time Complexity ($O(1)$)** for lookups, insertions, and deletions. Whether your dictionary has 10 items or 10 million, finding a value takes roughly the same amount of time.
Below is a simplified visualization. When you add a key-value pair, a hash function calculates an index. When you want to retrieve it later, that same hash function points directly to the location, skipping the need to search.
Note: Real-world hash functions are complex to minimize "collisions" (where two keys hash to the same index). Python handles these collisions automatically behind the scenes.
Beyond basic assignment, Python provides robust methods to handle missing keys safely. The
.get() method is particularly useful as it returns None (or a default value)
instead of crashing if the key doesn't exist.
# Creating a dictionary
my_dict = {'apple': 1, 'banana': 2, 'cherry': 3}
print(f"Original: {my_dict}")
# Safe Retrieval using .get()
# Returns 'Not Found' instead of crashing
print(f"Get 'date': {my_dict.get('date', 'Not Found')}")
# Iterating over Key-Value pairs
print("--- Iteration ---")
for key, value in my_dict.items():
print(f"{key}: {value}")
# Dictionary Comprehension (Creating a new dict from an existing one)
# Example: Doubling the values
doubled_dict = {k: v * 2 for k, v in my_dict.items()}
print(f"Doubled: {doubled_dict}")
Sets are unordered collections of unique elements. They are essentially dictionaries with only keys and no values. Sets are incredibly useful for two main tasks: removing duplicates from a list and performing mathematical set operations.
# 1. Removing duplicates from a list
raw_data = [1, 2, 2, 3, 4, 4, 4, 5]
unique_data = list(set(raw_data))
print(f"Unique data: {unique_data}")
# 2. Fast Membership Testing
# Checking 'if x in set' is O(1), much faster than checking a list
my_set = {1, 2, 3, 4, 5}
print(f"Is 3 in set? {3 in my_set}")
# Adding/Removing
my_set.add(6)
my_set.remove(3)
print(f"Updated Set: {my_set}")
Sets support operations like union, intersection, and difference. These are powerful for data analysis, such as finding commonalities between two datasets.
set1 = {1, 2, 3, 4, 5}
set2 = {4, 5, 6, 7, 8}
# Intersection: Elements common to both
print(f"Common: {set1.intersection(set2)}")
# Difference: Elements in set1 but NOT in set2
print(f"Unique to Set1: {set1.difference(set2)}")
# Symmetric Difference: Elements in either, but not both
print(f"Non-overlapping: {set1.symmetric_difference(set2)}")
Leveraging Generative AI to implement dictionaries and sets efficiently involves creating precise, goal-oriented prompts. Below are a few strategies:
Resulting AI-generated code:
# Shopping List Management Program
shopping_list = {}
def add_item(item, quantity):
# .get(item, 0) handles the case where item doesn't exist yet
current_qty = shopping_list.get(item, 0)
shopping_list[item] = current_qty + quantity
print(f'Updated "{item}": {current_qty} -> {shopping_list[item]}')
def remove_item(item):
# Using 'pop' is safer and allows capturing the removed value
removed_qty = shopping_list.pop(item, None)
if removed_qty:
print(f'Removed "{item}" (was {removed_qty})')
else:
print(f'"{item}" not found.')
def display_list():
if not shopping_list:
print("List is empty.")
return
print("--- Shopping List ---")
total_items = 0
for item, quantity in shopping_list.items():
print(f"- {item}: {quantity}")
total_items += quantity
print(f"Total items: {total_items}")
# Example Usage
add_item("Milk", 2)
add_item("Bread", 1)
add_item("Milk", 1) # Adds to existing
display_list()
remove_item("Bread")
display_list()