Dictionaries and Sets: Fast Lookups, Hashing and Key-Value Data
Searching for an item inside a list of one million records requires Python to scan the list item by item from the beginning. When you need instant lookups by a unique identifier (such as finding a server by its hostname) or when you need to strip duplicates from a dataset immediately, Python provides dictionaries (dict) and sets (set).
What Is a Dictionary (dict)?
A dictionary stores data as key-value pairs enclosed in curly braces {key: value}. Backed by hash tables, looking up a value by its key takes microseconds regardless of dictionary size.
- Keys must be unique and immutable (strings, numbers or tuples).
- Values can be any data type, including lists or nested dictionaries.
- Dictionaries preserve the exact insertion order in which keys were added.
Reading, Adding and Safely Accessing Dictionary Keys
server_config = {
"hostname": "srv-app-01",
"ip": "10.24.1.15",
"port": 8080
}
# Add or update keys
server_config["environment"] = "Production"
server_config["port"] = 8443
# Safe lookup with .get(key, default) prevents KeyError crashes
owner = server_config.get("owner", "Unassigned")
print(f"Host: {server_config['hostname']} | Owner: {owner}")
Iterating Over Dictionaries
| Method | What It Yields | Typical Loop Pattern |
|---|---|---|
.keys() |
All dictionary keys | for k in config: |
.values() |
All dictionary values | for v in config.values(): |
.items() |
(key, value) pairs as tuples |
for k, v in config.items(): |
What Is a Set (set)?
A set is an unordered collection of unique, immutable elements enclosed in curly braces {"a", "b", "c"}. Sets automatically discard duplicates and support mathematical set operations:
raw_ips = ["10.0.0.1", "10.0.0.2", "10.0.0.1", "10.0.0.3"]
unique_ips = set(raw_ips) # {'10.0.0.1', '10.0.0.2', '10.0.0.3'}
prod_pkgs = {"nginx", "postgresql", "redis"}
stage_pkgs = {"nginx", "postgresql", "pytest"}
common = prod_pkgs & stage_pkgs # Intersection: {'nginx', 'postgresql'}
missing_in_stage = prod_pkgs - stage_pkgs # Difference: {'redis'}
all_unique = prod_pkgs | stage_pkgs # Union
A Short Example: Counting Error Frequencies in Logs
error_stream = ["ERR_TIMEOUT", "ERR_AUTH", "ERR_TIMEOUT", "ERR_DB", "ERR_TIMEOUT"]
error_counts = {}
for code in error_stream:
error_counts[code] = error_counts.get(code, 0) + 1
for code, count in error_counts.items():
print(f"{code}: {count} occurrences")
Common Mistakes
- Using
d["key"]when a key might be missing: Used.get("key")when parsing optional fields to avoid unhandledKeyErrorexceptions. - Writing
s = {}to create an empty set: Empty braces{}create an empty dictionary. Uses = set()for an empty set. - Using a list as a dictionary key: Lists are mutable and unhashable; convert them to a
tupleif you need a composite key.
Key takeaways
- Dictionaries store
key: valuepairs backed by hash tables for instant lookups. - Use
.get(key, default)to read optional dictionary keys safely. - Use
for key, value in my_dict.items():to loop over keys and values together. - Sets strip duplicates immediately and compare collections using
&,-and|.