👪 The Collections Module¶
The collections
module is a very useful built-in Python module that implements specialized container data types, providing alternatives to Pythonโs general purpose built-in containers.
1. Counter¶
Counter is a dict subclass for counting objects:
In [2]:
Copied!
from collections import Counter
colors = ['blue', 'red', 'blue', 'green', 'red', 'blue']
counter = Counter(colors)
print(counter) # Outputs: Counter({'blue': 3, 'red': 2, 'green': 1})
from collections import Counter
colors = ['blue', 'red', 'blue', 'green', 'red', 'blue']
counter = Counter(colors)
print(counter) # Outputs: Counter({'blue': 3, 'red': 2, 'green': 1})
Counter({'blue': 3, 'red': 2, 'green': 1})
2. defaultdict¶
defaultdict
is a dict subclass that calls a factory function to supply missing values:
In [3]:
Copied!
from collections import defaultdict
color_counts = defaultdict(int)
colors = ['blue', 'red', 'blue', 'green', 'red', 'blue']
for color in colors:
color_counts[color] += 1
print(color_counts) # Outputs: defaultdict(<class 'int'>, {'blue': 3, 'red': 2, 'green': 1})
from collections import defaultdict
color_counts = defaultdict(int)
colors = ['blue', 'red', 'blue', 'green', 'red', 'blue']
for color in colors:
color_counts[color] += 1
print(color_counts) # Outputs: defaultdict(, {'blue': 3, 'red': 2, 'green': 1})
defaultdict(<class 'int'>, {'blue': 3, 'red': 2, 'green': 1})
3. OrderedDict¶
An OrderedDict
is a dict subclass that remembers the order entries were added:
In [4]:
Copied!
from collections import OrderedDict
favorite_colors = OrderedDict()
favorite_colors['Tom'] = 'blue'
favorite_colors['Anna'] = 'red'
favorite_colors['John'] = 'green'
for name, color in favorite_colors.items():
print(f"{name}'s favorite color is {color}")
from collections import OrderedDict
favorite_colors = OrderedDict()
favorite_colors['Tom'] = 'blue'
favorite_colors['Anna'] = 'red'
favorite_colors['John'] = 'green'
for name, color in favorite_colors.items():
print(f"{name}'s favorite color is {color}")
Tom's favorite color is blue Anna's favorite color is red John's favorite color is green
4. namedtuple¶
namedtuple
generates a subclass of tuple that has named fields, an alternative to namedtuple is the dataclass
decorator, which is available in Python 3.7 and newer.
In [5]:
Copied!
from collections import namedtuple
# Define a new tuple subclass named 'Color'
Color = namedtuple('Color', ['red', 'green', 'blue'])
color = Color(blue=55, green=155, red=255)
print(color.red) # Outputs: 255
from collections import namedtuple
# Define a new tuple subclass named 'Color'
Color = namedtuple('Color', ['red', 'green', 'blue'])
color = Color(blue=55, green=155, red=255)
print(color.red) # Outputs: 255
255
5. deque¶
deque
is a list-like container with fast appends and pops on either end:
In [6]:
Copied!
from collections import deque
# Create a deque
d = deque('ghi') # make a new deque with three items
for elem in d: # iterate over the deque's elements
print(elem.upper())
from collections import deque
# Create a deque
d = deque('ghi') # make a new deque with three items
for elem in d: # iterate over the deque's elements
print(elem.upper())
G H I
The collections
module is very useful for a variety of tasks and can make your code cleaner and faster.