Real-World Python Collection Examples

See Python collections in action with real-world examples: counting words, grouping data, removing duplicates, building lookup tables, and processing JSON.

5 min read

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.

pythonpython
text = "the cat and the dog and the bird"
words = text.split()
counts = {}
for word in words:
    counts[word] = counts.get(word, 0) + 1

The 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.

pythonpython
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.

pythonpython
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.

pythonpython
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

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.
RunePowered by Rune AI

Frequently Asked Questions

How do I count word frequencies in a text using Python?

Use a dictionary where each word is a key and the count is the value. Loop through the words and for each word, increment its count. You can use a plain dict with get() to provide a default of 0, or use a defaultdict with int as the factory. The Counter class from the collections module is purpose-built for this task and provides the most common frequency operations in a single call.

How do I group a list of records by a field?

Use a defaultdict with list as the factory. Iterate over the records, extract the grouping field, and append each record to the list for that field. This groups records in a single pass and produces a dictionary where each key maps to a list of records belonging to that group.

What is the easiest way to remove duplicates from a list?

Pass the list to the set constructor, which discards duplicates automatically. If you need to preserve the original order, use a dictionary (Python 3.7+) where the items are keys, since dicts preserve insertion order and deduplicate keys: list(dict.fromkeys(my_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.