Exploring Python's Collections Module

PythonPythonBeginner
Practice Now

Introduction

In this tutorial, we will explore Python's built-in collections module. The collections module is a powerful library that offers a variety of container data types that extend the functionality of Python's built-in containers such as lists, tuples, and dictionaries.

NamedTuple

A namedtuple is a subclass of a tuple, providing named fields for more readability and self-documenting code. Let's create a namedtuple in named_tuple.py to represent a point in a 2D space:

## Import collections
from collections import namedtuple

## Define a namedtuple type Point with x and y properties
Point = namedtuple('Point', ['x', 'y'])

## Create a Poinit object
p = Point(1, 2)

## Retrieve the properties of point
print(p.x)
print(p.y)

Then, please run the script in the terminal:

python named_tuple.py

Output:

1
2

Counter

Counter is a subclass of dict that counts the occurrences of elements in a collection. Let's create a Counter object in counter.py to count the occurrences of characters in a string:

from collections import Counter

text = "hello, world!"
## Gets the number of occurrences of the elements in the collection and returns them as a dictionary
char_count = Counter(text)

print(char_count)

Then, please run the script in the terminal:

python counter.py

Output:

Counter({'l': 3, 'o': 2, 'h': 1, 'e': 1, ',': 1, ' ': 1, 'w': 1, 'r': 1, 'd': 1, '!': 1})

OrderedDict

OrderedDict is a subclass of dict that maintains the order of elements as they were inserted. Let's create an OrderedDict in ordered_dict.py and add some key-value pairs:

from collections import OrderedDict

## Initialdefining OrderedDict
od = OrderedDict()

## Insert in key-value pairs
od['a'] = 1
od['b'] = 2
od['c'] = 3

## Iterate over the key-value pairs and print out the contents
for key, value in od.items():
    print(key, value)

Then, please run the script in the terminal:

python ordered_dict.py

Output:

a 1
b 2
c 3

DefaultDict

Defaultdict(int)

DefaultDict is a subclass of dict that provides a default value for a nonexistent key. Let's create a DefaultDict with default values 0 in default_dict1.py and count the occurrences of words in a sentence:

from collections import defaultdict

sentence = "the quick brown fox jumps over the lazy dog"
word_count1 = defaultdict(int)

for word in sentence.split():
    ## Count the occurrences of words
    word_count1[word] += 1

print(dict(word_count1))

Then, please run the script in the terminal:

python default_dict1.py

Output:

{'the': 2, 'quick': 1, 'brown': 1, 'fox': 1, 'jumps': 1, 'over': 1, 'lazy': 1, 'dog': 1}

If we didn't use DefaultDict, the appeal code would look like this:

sentence = "the quick brown fox jumps over the lazy dog"
result = {}

for word in sentence.split():
    if word in result:
        result[word] += 1
    else:
        result[word] = 1

print(result)

Defaultdict(list)

Next, Let's create a DefaultDict with default values [] in default_dict2.py and store the number in each letter:

from collections import defaultdict

data = [('a', 1), ('a', 1), ('a', 3), ('b', 1), ('b', 2), ('b', 3)]
word_count2 = defaultdict(list)

for (key,value) in data:
    ## Store the number in each letter
    word_count2[key].append(value)

print(dict(word_count2))

Then, please run the script in the terminal:

python default_dict2.py

Output:

{'a': [1, 1, 3], 'b': [1, 2, 3]}

If we didn't use DefaultDict, the appeal code would look like this:

data = [('a', 1), ('a', 1), ('a', 3), ('b', 1), ('b', 2), ('b', 3)]
result = {}

for (key, value) in data:
    if key in result:
        result[key].append(value)
    else:
        result[key] = [value]

print(result)

Defaultdict(set)

Finally, Let's create a DefaultDict with default values set() in default_dict3.py and stores the number that is not repeated in each letter:

from collections import defaultdict

data = [('a', 1), ('a', 1), ('a', 3), ('b', 1), ('b', 2), ('b', 3)]
word_count3 = defaultdict(set)

for (key,value) in data:
    ## Stores the number that is not repeated in each letter
    word_count3[key].add(value)

print(dict(word_count3))

Then, please run the script in the terminal:

python default_dict3.py

Output:

{'a': {1, 3}, 'b': {1, 2, 3}}

If we didn't use DefaultDict, the appeal code would look like this:

data = [('a', 1), ('a', 1), ('a', 3), ('b', 1), ('b', 2), ('b', 3)]
result = {}

for (key, value) in data:
    if key in result:
        result[key].add(value)
    else:
        result[key] = {value}

print(result)

Deque

A deque (double-ended queue) is a generalization of stacks and queues that supports fast O(1) appends and pops from both ends. Let's create a deque in deque.py and perform some operations:

from collections import deque

d = deque([1, 2, 3, 4, 5])

## Append to the right
d.append(6)
print("Append to the right:", d)

## Append to the left
d.appendleft(0)
print("Append to the left:", d)

## Pop from the right
right_element = d.pop()
print("The right element:", right_element)
print("Pop from the right:", d)

## Pop from the left
left_element = d.popleft()
print("The left element:", left_element)
print("Pop from the left:", d)

## Rotate the deque
d.rotate(2)
print("Rotate clockwise the deque:", d)

d.rotate(-2)
print("Rotate counterclockwise the deque:", d)

Then, please run the script in the terminal:

python deque.py

Output:

Append to the right: deque([1, 2, 3, 4, 5, 6])
Append to the left: deque([0, 1, 2, 3, 4, 5, 6])
The right element: 6
Pop from the right: deque([0, 1, 2, 3, 4, 5])
The left element: 0
Pop from the left: deque([1, 2, 3, 4, 5])
Rotate clockwise the deque: deque([4, 5, 1, 2, 3])
Rotate counterclockwise the deque: deque([1, 2, 3, 4, 5])

Summary

In this tutorial, we covered the main classes provided by the collections module, including namedtuple, Counter, OrderedDict, DefaultDict, and deque. These classes can be useful for various tasks and are great additions to your Python toolkit.

Other Python Tutorials you may like