collections — Easy Examples
Specialized containers: deque, Counter, defaultdict, OrderedDict, ChainMap, namedtuple
Getting started with collections
Basic import and usage of the collections module.
python
from collections import Counter, defaultdict # Counter words = ["apple", "banana", "apple", "cherry", "banana", "apple"] c = Counter(words) print(c) print(c.most_common(2)) # defaultdict dd = defaultdict(list) dd["fruits"].append("apple") dd["fruits"].append("banana") print(dict(dd))
The collections module is part of Python's standard library. Specialized containers: deque, Counter, defaultdict, OrderedDict, ChainMap, namedtuple.
Common collections operations
Frequently used functions from the collections module.
python
# More collections examples import collections print(f"collections module loaded successfully") print(f"Location: {collections.__name__}") print(f"Has {len(dir(collections))} attributes")
These are the most commonly used features of the collections module.
Want to try these examples interactively?
Open Easy Playground