Tutorials Logic, IN info@tutorialslogic.com

Python collections and itertools: Counters, Queues, Grouping, and Lazy Pipelines

Standard Containers

The built-in list, tuple, set, and dictionary cover most programs. The collections module supplies focused containers for counting, queues, defaults, and lightweight records; itertools supplies lazy building blocks for combining and slicing iterables.

After this lesson, you can choose Counter, deque, defaultdict, islice, chain, and groupby for their actual behavior and avoid materializing a sequence when the consumer only needs part of it.

Counter Frequencies

Counter maps values to occurrence counts and supports total(), most_common(), and multiset-style arithmetic. Missing keys read as zero rather than raising KeyError.

Count Course Tags

Count Course Tags
from collections import Counter

tags = ["python", "sql", "python", "testing", "python", "sql"]
counts = Counter(tags)

print(counts.most_common(2))
Output
[('python', 3), ('sql', 2)]

Deque Queue

deque supports efficient appends and pops at both ends. Use append() with popleft() for a FIFO queue; a list pop(0) shifts remaining elements.

Process Oldest Task First

Process Oldest Task First
from collections import deque

tasks = deque(["validate", "save"])
tasks.append("notify")

print(tasks.popleft())
print(list(tasks))
Output
validate
['save', 'notify']

Default Grouping

defaultdict calls a factory when a missing key is accessed. Use it when automatic creation is the intended mutation; use dict.get() when a lookup should not change the mapping.

Group Lessons by Level

Group Lessons by Level
from collections import defaultdict

groups = defaultdict(list)
for level, lesson in [("beginner", "Lists"), ("advanced", "Async")]:
    groups[level].append(lesson)

print(groups["beginner"])
Output
['Lists']

Lazy Composition

itertools.chain joins iterables without concatenating them, while islice takes a bounded window without requiring sequence slicing. These tools preserve laziness when their inputs are lazy.

Read a Combined Window

Read a Combined Window
from itertools import chain, islice

combined = chain([1, 2], range(3, 100))
print(list(islice(combined, 5)))
Output
[1, 2, 3, 4, 5]

Groupby Ordering

groupby groups adjacent equal keys, not every matching key in the entire input. Sort by the same key first when records are not already grouped, and consume each group before advancing the outer iterator.

Choose the Container

0 of 2 checked

Q1. Which structure supports efficient FIFO removal?

Q2. What does groupby combine?

Try this next

Use a Standard Tool

0 of 2 completed

  1. Use deque(maxlen=5) and prove which item disappears after a sixth append.
  2. Use islice to consume batches from a row iterator without loading the complete file.
Browse Free Tutorials

Explore 500+ free tutorials across 20+ languages and frameworks.