Python collections Module

The collections module provides specialized container datatypes that extend Python's built-in dict, list, tuple, and set. These containers are optimized for specific access patterns and offer cleaner, more expressive code.

Use Cases:
- Counting occurrences of elements with Counter
- Providing default values for missing dictionary keys with defaultdict
- Maintaining insertion order with OrderedDict (relevant for Python < 3.7)
- Creating lightweight record types with namedtuple
- Building efficient queues with deque (double-ended queue)

Example 1: Counting word frequencies
    from collections import Counter
    words = "the cat sat on the mat the cat".split()
    freq = Counter(words)
    print(freq.most_common(3))
    # [('the', 3), ('cat', 2), ('sat', 1)]

Example 2: Grouping items with defaultdict
    from collections import defaultdict
    students = [("math", "Alice"), ("science", "Bob"), ("math", "Charlie")]
    by_subject = defaultdict(list)
    for subject, name in students:
        by_subject[subject].append(name)
    print(dict(by_subject))
    # {'math': ['Alice', 'Charlie'], 'science': ['Bob']}

Other useful types include ChainMap for searching multiple dictionaries as one, and UserDict/UserList/UserString for creating custom container subclasses. The Counter class supports arithmetic operations like addition and subtraction of counts.
