Working with Collections Module

Loading

The collections module in Python provides specialized container data types that extend the functionality of standard lists, tuples, dictionaries, and sets. These specialized containers offer better performance, readability, and flexibility for specific use cases.

Why Use collections?

Optimized for performance
Easier and more readable syntax
Useful for handling complex data structures


1. Common Data Structures in collections Module

The collections module includes several useful classes:

  1. Counter → Counts occurrences of elements
  2. defaultdict → Provides default values for missing keys
  3. OrderedDict → Maintains the order of dictionary keys
  4. deque → Fast list-like structure optimized for appends/pops
  5. namedtuple → Immutable, readable tuples with named fields
  6. ChainMap → Combines multiple dictionaries into one

2. Counter – Counting Elements

Counter is a dictionary subclass that counts hashable objects.

Example: Counting Words in a String

from collections import Counter

text = "apple banana apple orange banana apple"
word_count = Counter(text.split())
print(word_count)

Output:

Counter({'apple': 3, 'banana': 2, 'orange': 1})

Example: Finding Most Common Elements

fruits = ["apple", "banana", "orange", "banana", "apple", "apple"]
counter = Counter(fruits)

print(counter.most_common(2)) # Top 2 most common elements

Output:

[('apple', 3), ('banana', 2)]

3. defaultdict – Handling Missing Keys

A defaultdict is a dictionary that assigns a default value to missing keys.

Example: Using defaultdict with int

from collections import defaultdict

numbers = defaultdict(int) # Default value is 0
numbers["a"] += 1
print(numbers)

Output:

defaultdict(<class 'int'>, {'a': 1})

Example: Grouping Data with defaultdict

data = [("Alice", "Math"), ("Bob", "Science"), ("Alice", "English")]

subjects = defaultdict(list)
for name, subject in data:
subjects[name].append(subject)

print(subjects)

Output:

defaultdict(<class 'list'>, {'Alice': ['Math', 'English'], 'Bob': ['Science']})

4. OrderedDict – Maintaining Key Order

An OrderedDict maintains the insertion order of keys (Python 3.7+ does this for normal dictionaries too).

Example: Using OrderedDict

from collections import OrderedDict

ordered_dict = OrderedDict()
ordered_dict["a"] = 1
ordered_dict["b"] = 2
ordered_dict["c"] = 3

print(ordered_dict)

Output:

OrderedDict([('a', 1), ('b', 2), ('c', 3)])

5. deque – Fast Appends and Pops

A deque (double-ended queue) allows fast appends and pops from both ends, unlike a list.

Example: Using deque

from collections import deque

d = deque([1, 2, 3])
d.append(4) # Append to the right
d.appendleft(0) # Append to the left
print(d)

Output:

deque([0, 1, 2, 3, 4])

Example: Removing Elements from deque

d.pop()  # Removes from the right
d.popleft() # Removes from the left
print(d)

Output:

deque([1, 2, 3])

6. namedtuple – Readable Tuples with Names

A namedtuple provides named fields for tuples, making them more readable.

Example: Defining a namedtuple

from collections import namedtuple

Person = namedtuple("Person", ["name", "age"])
p = Person("Alice", 25)

print(p.name, p.age)

Output:

Alice 25

Example: Accessing Elements Like a Dictionary

print(p._asdict())  # Converts namedtuple to dictionary

Output:

{'name': 'Alice', 'age': 25}

7. ChainMap – Merging Multiple Dictionaries

A ChainMap combines multiple dictionaries into one logical view.

Example: Merging Configuration Settings

from collections import ChainMap

default_settings = {"theme": "light", "language": "English"}
user_settings = {"language": "French"}

settings = ChainMap(user_settings, default_settings)
print(settings["language"]) # Takes the value from user_settings

Output:

French

8. Performance Comparison

Data StructureUse CasePerformance Benefit
CounterCounting elementsFaster than loops
defaultdictHandling missing keysAvoids KeyErrors
OrderedDictKeeping key orderUseful in older Python versions
dequeFast appends/removalsFaster than lists
namedtupleReadable tuplesLightweight and structured
ChainMapCombining dictionariesAvoids merging manually

9. When to Use the collections Module?

✅ Use Counter when you need to count occurrences.
✅ Use defaultdict when handling missing keys.
✅ Use OrderedDict when order matters.
✅ Use deque when you need fast queue operations.
✅ Use namedtuple for structured, readable tuples.
✅ Use ChainMap to merge dictionaries dynamically.

🚫 Avoid overcomplicating your code with collections unless necessary.

Leave a Reply

Your email address will not be published. Required fields are marked *