The articles in this section have covered each collection type individually: how to create them, access them, modify them, and choose between them. These real-world Python collection examples bring everything together by showing collections in real scenarios, the kind of small programs and data-processing tasks that make up a typical day of Python programming. Each example pairs a common problem with the collection type and method combination that solves it most naturally.
If you have worked through the entire Collections section, you will recognize every technique used here. Word counting uses dictionaries the way the article on working with Python dictionaries described. Grouping uses defaultdict the way the article on improving Python collection performance recommended. Deduplication uses sets the way the earlier sections on working with Python sets introduced. These are not new concepts; they are the concepts you have already learned, applied to realistic problems.
Counting word frequencies
Counting how many times each word appears in a text is the classic dictionary exercise. You split the text into words, iterate over them, and maintain a dictionary where each word maps to its running count. The get method with a default of zero handles the first occurrence of each word cleanly.
text = "the cat and the dog and the bird"
words = text.split()
counts = {}
for word in words:
counts[word] = counts.get(word, 0) + 1The result is {"the": 3, "cat": 1, "and": 2, "dog": 1, "bird": 1}. Each word appears as a key with its frequency as the value. The get method returns 0 for new words, which becomes 1 after the addition, and returns the current count for existing words, which increments by one.
The Counter class from the collections module provides the same result in one line and adds convenience methods for finding the most common elements, combining counts, and subtracting one counter from another. For production code, Counter is the right choice. For learning purposes, the dictionary version reveals exactly how the counting mechanism works.
Grouping records by a field
Grouping a flat list of records into categories is a task that appears in report generation, data analysis, and any program that presents summary information. A defaultdict with list as the factory collects records into groups in a single pass without checking whether each group key already exists.
from collections import defaultdict
orders = [
{"category": "books", "amount": 45},
{"category": "electronics", "amount": 200},
{"category": "books", "amount": 30},
]
by_category = defaultdict(list)
for order in orders:
by_category[order["category"]].append(order)The result is a dictionary where "books" maps to a list of two order dictionaries and "electronics" maps to a list of one. From here you can compute totals per category, count orders per category, or filter to categories that meet a threshold. The defaultdict handles the first occurrence of each category silently, creating an empty list on demand.
Removing duplicates while preserving order
Removing duplicates from a list is the most common use of sets. Passing a list to the set constructor discards duplicates instantly. The tradeoff is that sets do not preserve insertion order, so the resulting unique items are in an arbitrary sequence.
When order matters, use a dictionary in Python 3.7 or later. The fromkeys method creates a dictionary from a sequence of keys, and because dictionaries preserve insertion order and keys must be unique, the result contains each item exactly once in its first-seen order.
items = ["apple", "banana", "apple", "cherry", "banana"]
unique_ordered = list(dict.fromkeys(items))The result is ["apple", "banana", "cherry"], deduplicated and in the order the items first appeared. This pattern is concise, fast, and relies only on built-in behavior that has been part of the language guarantee since Python 3.7.
Building a lookup table
A lookup table maps identifiers to records so you can retrieve any record by its identifier in constant time. This is the fundamental use case for dictionaries, and it appears in every program that processes data with unique IDs.
Given a list of user records, build a dictionary keyed by user ID with a dictionary comprehension. Any subsequent operation that needs a user by ID retrieves it from the dictionary instead of scanning the list.
users = [
{"id": 101, "name": "Alice"},
{"id": 205, "name": "Bob"},
]
by_id = {user["id"]: user for user in users}
print(by_id[205]["name"])The comprehension iterates over the list once, using each record's id field as the dictionary key. After that one-time cost, looking up by_id[205] is a constant-time hash lookup instead of a linear scan through the original list. This pattern scales well: the lookup stays just as fast whether the list holds a hundred records or a million.
Processing JSON data
JSON is the universal data interchange format of the web, and Python's json module parses JSON directly into nested dictionaries and lists. A JSON object becomes a dictionary. A JSON array becomes a list. Nested JSON becomes nested dictionaries and lists layered inside each other, the same multi-level structures covered earlier in this section.
Accessing fields in parsed JSON uses the same square-bracket syntax and chained get calls covered throughout this section. Safely navigating deeply nested JSON where intermediate keys may be absent is the real-world application of the safe dictionary access patterns you learned earlier.
The article that follows, practice building with Python collections, provides hands-on exercises that apply these patterns to build complete, working programs from scratch.
Rune AI
Key Insights
- Count frequencies with a dictionary or Counter; use get() or defaultdict(int) for clean incrementing.
- Group records by a field with defaultdict(list), appending each record to its group in a single pass.
- Remove duplicates with set() for speed or dict.fromkeys() to preserve insertion order.
- Build lookup tables with a dictionary mapping IDs to records for constant-time retrieval.
- Parse JSON directly into nested Python dictionaries and lists with the json module.
Frequently Asked Questions
How do I count word frequencies in a text using Python?
How do I group a list of records by a field?
What is the easiest way to remove duplicates from a list?
Conclusion
The collection concepts covered throughout this section come together in real programs. Word counting, grouping, deduplication, and lookup tables are the patterns you will write most often, and each one maps naturally to a specific collection type and method combination.
More in this topic
Define and Call Python Functions
Learn the exact syntax for defining and calling Python functions with the def keyword, including naming rules, the function body, and how the call stack works.
Python Function Parameters and Arguments
Learn the difference between parameters and arguments, how to pass values by position and by keyword, and the rules for default parameter values.
Python Functions Explained
Learn what Python functions are, why they matter for organizing code, and how they form the foundation of non-trivial programs.