Python Collections Module

The Python `collections` module provides specialized container datatypes, beyond the common `list`, `tuple`, `dict`, and `set`. This enhances the possibilities for storing and managing data in your programs.

Example 1: `namedtuple`


    from collections import namedtuple
    
    # Create a namedtuple
    Point = namedtuple('Point', ['x', 'y'])
    
    # Create instances
    p1 = Point(10, 20)
    p2 = Point(30, 40)
    
    print(p1.x, p1.y)  # Output: 10 20
    print(p1) # Output: Point(x=10, y=20)
        

Creates a `Point` type that provides named fields (`x`, `y`) instead of just numeric indices. This improves readability.

Example 2: `Counter`


    from collections import Counter
    
    words = "apple banana apple cherry banana apple".split()
    word_counts = Counter(words)
    print(word_counts)  # Output: Counter({'apple': 3, 'banana': 2, 'cherry': 1})
        

Efficiently counts the occurrences of items in a sequence.

Example 3: `deque` (Double-ended queue)


    from collections import deque
    
    queue = deque([1, 2, 3])
    queue.append(4)
    queue.appendleft(0)
    print(queue)  # Output: deque([0, 1, 2, 3, 4])
    queue.pop()
    print(queue) # Output: deque([0, 1, 2, 3])
        

Provides efficient appending and popping from both ends of a sequence. Useful for queues and stacks.

Example 4: `defaultdict`


    from collections import defaultdict
    
    d = defaultdict(int)
    d['a'] += 1
    d['b'] += 2
    d['a'] += 1
    print(d['a']) # Output: 2
    print(d['c']) # Output: 0
        

Creates a dictionary with a default value if a key doesn't exist. Here, a default value of 0 is assigned if a key is not found.

Example 5: `OrderedDict`


    from collections import OrderedDict
    
    d = OrderedDict()
    d['a'] = 1
    d['b'] = 2
    d['c'] = 3
    for key, value in d.items():
        print(key, value)
        

Maintains the order of insertion of key-value pairs. Crucial for scenarios where order matters.

Important Considerations

  • `namedtuple`: Enhances readability when working with records.
  • `Counter`: Excellent for counting items in sequences.
  • `deque`: More efficient for operations at both ends of sequences compared to lists.
  • `defaultdict`: Avoids `KeyError` if a key doesn't exist, specifying a default value.
  • `OrderedDict`: Preserves the insertion order of elements, essential for specific scenarios like logging.