Dictionaries & sets
Key–value maps, uniqueness, counting, grouping, and fast lookups.
How to Create a Dict from Two Parallel Lists in Python (zip)
Build a dictionary by pairing elements from two parallel lists using Python's built-in zip function and dict constructor.
keys = ["name", "age", "city"]
values = ["Alice", 30, "New York"]
result = dict(zip(keys, values))
print(result)
How to Group a List of Dictionaries by Key in Python
Group a list of dictionaries by a specified key field using dict.setdefault to build a dictionary of lists.
def group_by_key(records, key):
grouped = {}
for record in records:
grouped.setdefault(record[key], []).append(record)
return grouped
if __name__ == "__main__":
data = [
{"name": "Alice", "dept": "engineering"},
{"name": "Bob", "dept": "sales"},
{"name": "Carol", "dept"…
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.