The collections Module for Specialized Container Types in Python


Use advanced container types for various needs.

Source Code

from collections import defaultdict, namedtuple, Counter, deque

# defaultdict: returns a default value for missing keys
dd = defaultdict(int)

# namedtuple: lightweight object type
Point = namedtuple('Point', ['x', 'y'])

# Counter: dict subclass for counting hashable objects
cnt = Counter()

# deque: list-like container with fast appends and pops on either end
dq = deque()
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments