Dictionaries & sets
Key–value maps, uniqueness, counting, grouping, and fast lookups.
How to Compute Set Union of Tags from Multiple Items in Python
Collect all unique tags from a list of dictionaries using set union with update() in Python.
items = [
{"id": 1, "tags": {"python", "web"}},
{"id": 2, "tags": {"web", "api", "sql"}},
{"id": 3, "tags": {"python", "data"}},
]
def get_union_of_tags(item_list):
all_tags = set()
for item in item_list:
all_tags.update(item["tags"])
return all_tags
if __name__ == "__main__":
u…
How to Implement Disjoint Set Union Find in Python
Implement a Disjoint Set Union-Find data structure using a Python dictionary for parent tracking, with path compression and connectivity checks.
class DisjointSet:
def __init__(self):
self.parent = {}
def find(self, x):
# Path compression
if self.parent[x] != x:
self.parent[x] = self.find(self.parent[x])
return self.parent[x]
def union(self, x, y):
# Initialize if not present
if x not in…
Browse by section
Each section groups closely related Python snippets.
Dictionaries & sets — Python code examples
What you will find here
This page collects dictionaries & sets snippets — short, copy-ready Python you can paste into our free online IDE and run without installing anything. Each sample includes a plain-English explanation and the full source code.
Samples vs tutorials and challenges
Samples are quick reference — one concept per page. For step-by-step teaching, use our Python tutorials. To test yourself, try quizzes or coding challenges. Clean up style with the Python formatter.