

Photo by Editor | Chat GPT
Introduction
Offer a number of modules to effectively perform joint tasks.
In them, collections
The module is a standout example, providing special data types of containers that can serve as an alternative to the built -in containers for the general purpose of the Ujjar. dict
For, for, for,. list
For, for, for,. set
And tuple
. Although many developers are familiar with some of its components, the module hosts a variety of utilities that are amazingly useful and can facilitate the code, improve reading ability, and promote performance.
Ten practical in this tutorial – and probably surprised – Azigar’s applications collections
Module
1. Easily counting the hashable items with the counter
A common job in almost any data analysis project is counting the presence of items in a setting. collections.Counter
The class is specially designed for it. This is a dictionary sub -class where elements are stored as keys and their count is protected as values.
from collections import Counter
# Count the frequency of words in a list
words = ('galaxy', 'nebula', 'asteroid', 'comet', 'gravitas', 'galaxy', 'stardust', 'quasar', 'galaxy', 'comet')
word_counts = Counter(words)
# Find the two most common words
most_common = word_counts.most_common(2)
# Output results
print(f"Word counts: c2")
print(f"Most common words: c2")
Output:
Word counts: Counter(c1 )
Most common words: (('galaxy', 3), ('comet', 2))
Make a lightweight class with 2 namedtuple
When you need a simple class for data grouping without any ways, A namedtuple
A useful, memory is effective option. It allows you to make items like a tuple that makes the fields accessible to the search for attribution as well as the index and not capable. This enables your code to read more than the standard tuple use.
from collections import namedtuple
# Define a Book namedtuple
# Fields: title, author, year_published, isbn
Book = namedtuple('Book', ('title', 'author', 'year_published', 'isbn'))
# Create an instance of the Book
my_book = Book(
title="The Hitchhiker\"s Guide to the Galaxy',
author="Douglas Adams",
year_published=1979,
isbn='978-0345391803'
)
print(f"Book Title: c1 ")
print(f"Author: {my_book.author}")
print(f"Year Published: {my_book.year_published}")
print(f"ISBN: {my_book.isbn}")
print("\n--- Accessing by index ---")
print(f"Title (by index): {my_book(0)}")
print(f"Author (by index): {my_book(1)}")
print(f"Year Published (by index): {my_book(2)}")
print(f"ISBN (by index): {my_book(3)}")
Output:
Accessing book data by field name
Title (by field name): The Hitchhiker's Guide to the Galaxy
Author (by field name): Douglas Adams
Year Published (by field name): 1979
ISBN (by field name): 978-0345391803
Accessing book data by index
Title (by index): The Hitchhiker's Guide to the Galaxy
Author (by index): Douglas Adams
Year Published (by index): 1979
ISBN (by index): 978-0345391803
You can think of A namedtuple
Like a variable C -structure, or as a data class without methods. They are definitely used.
3. Lost dictionary keys handle beautifully defaultdict
A common frustration when working with darling KeyError
This happens when you try to access a key that does not exist. collections.defaultdict
The perfect solution. This is a subclass dict
Which is called a factory function for the supply of lost keys. This is especially useful for grouping of items.
from collections import defaultdict
# Group a list of tuples by the first element
scores_by_round = (('contestantA', 8), ('contestantB', 7), ('contestantC', 5),
('contestantA', 7), ('contestantB', 7), ('contestantC', 6),
('contestantA', 9), ('contestantB', 5), ('contestantC', 4))
grouped_scores = defaultdict(list)
for key, value in scores_by_round:
grouped_scores(key).append(value)
print(f"Grouped scores: {grouped_scores}")
Output:
Grouped scores: defaultdict(, {'contestantA': (8, 7, 9), 'contestantB': (7, 7, 5), 'contestantC': (5, 6, 4)})
4. Following up with sharp rows and steaks deque
The lists of Azgar can be used as a stack and a row, though they are not good for these operations. The end of a list is faster to death and pope, but from the beginning it is slow to do the same because all other elements have to be transmitted. collections.deque
(Double End Row) is designed for high -speed supplements and paps from both ends.
First, here is an example of a row deque
.
from collections import deque
# Create a queue
d = deque((1, 2, 3))
print(f"Original queue: {d}")
# Add to the right
d.append(4)
print("Adding item to queue: 4")
print(f"New queue: {d}")
# Remove from the left
print(f"Popping queue item (from left): {d.popleft()}")
# Output final queue
print(f"Final queue: {d}")
& nbsp
Output:
Original queue: deque((1, 2, 3))
Adding item to queue: 4
New queue: deque((1, 2, 3, 4))
Popping queue item (from left): 1
Final queue: deque((2, 3, 4))
And use now deque
To make a stack:
from collections import deque
# Create a stack
d = deque((1, 2, 3))
print(f"Original stack: {d}")
# Add to the right
d.append(5)
print("Adding item to stack: 5")
print(f"New stack: {d}")
# Remove from the right
print(f"Popping stack item (from right): {d.pop()}")
# Output final stack
print(f"Final stack: {d}")
Output:
Original stack: deque((1, 2, 3))
Adding item to stack: 5
New stack: deque((1, 2, 3, 5))
Popping stack item (from right): 5
Final stack: deque((1, 2, 3))
5. Remembering the enrollment order OrderedDict
Before 3.7, standard dictionaries did not save the sequence in which the items were entered. To solve this, collections.OrderedDict
Was used. While the standard duct now maintains the entry order, OrderedDict
Still have unique features, such as move_to_end()
Method, which is useful for tasks such as making a simple cache.
from collections import OrderedDict
# An OrderedDict remembers the order of insertion
od = OrderedDict()
od('a') = 1
od('b') = 2
od('c') = 3
print(f"Start order: {list(od.keys())}")
# Move 'a' to the end
od.move_to_end('a')
print(f"Final order: {list(od.keys())}")
Output:
Start order: ('a', 'b', 'c')
Final order: ('b', 'c', 'a')
6. In combination with multiple dictionary ChainMap
collections.ChainMap
The class provides a way to connect multiple dictionaries together so that they can be considered a unit. This is much faster than creating a new dictionary and running multiple update()
Calls find basic maps one by one until a key is found.
Let’s create a chain map called China and ask for its keys.
from collections import ChainMap
# Create dictionaries
dict1 = {'a': 1, 'b': 2}
dict2 = {'b': 3, 'c': 4}
# Create a ChainMap
chain = ChainMap(dict1, dict2)
# Print dictionaries
print(f"dict1: {dict1}")
print(f"dict2: {dict2}")
# Query ChainMap for keys and return values
print("\nQuerying ChainMap for keys")
print(f"a: {chain('a')}")
print(f"c: {chain('c')}")
print(f"b: {chain('b')}")
Output:
dict1: {'a': 1, 'b': 2}
dict2: {'b': 3, 'c': 4}
Querying keys for values
a: 1
c: 4
b: 2
Note that, in the above scenario, ‘B’ is found in the first dict1
The first dictionary in chain
And so this is the value associated with the key that has returned.
7. Keeping a limited date with deck maxlen
A deque
Using can be formed with a fixed maximum length maxlen
The argument is added to the maximum length, the opposite ends are automatically wasted. This is perfect for maintaining the date of the last N item.
from collections import deque
# Keep a history of the last 3 items
history = deque(maxlen=3)
history.append("cd ~")
history.append("ls -l")
history.append("pwd")
print(f"Start history: {history}")
# Add a new item, push out the left-most item
history.append("mkdir data")
print(f"Final history: {history}")
Output:
Start history: deque(('cd ~', 'ls -l', 'pwd'), maxlen=3)
Final history: deque(('ls -l', 'pwd', 'mkdir data'), maxlen=3)
8. Making the domestic dictionary easily defaultdict
On the building defaultdict
You can easily create dictionary like domestic or tree. By providing A lambda
Function that looted another defaultdict
You can create a dictionary dictionary on the fly.
from collections import defaultdict
import json
# A function that returns a defaultdict
def tree():
return defaultdict(tree)
# Create a nested dictionary
nested_dict = tree()
nested_dict('users')('user1')('name') = 'Felix'
nested_dict('users')('user1')('email') = 'user1@example.com'
nested_dict('users')('user1')('phone') = '515-KL5-5555'
# Output formatted JSON to console
print(json.dumps(nested_dict, indent=2))
Output:
{
"users": {
"user1": {
"name": "Felix",
"email": "user1@example.com",
"phone": "515-KL5-5555"
}
}
}
9. Performing on the math operations Counters
News Flash: You can perform math work, such as addiction, reduction, intersection and union, Counter
Objects This is a powerful tool to compare and combine the frequency of frequency from various sources.
from collections import Counter
c1 = Counter(a=4, b=2, c=0, d=-2)
c2 = Counter(a=1, b=2, c=3, d=4)
# Add counters -> adds counts for common keys
print(f"c1 + c2 = {c1 + c2}")
# Subtract counters -> keeps only positive counts
print(f"c1 - c2 = {c1 - c2}")
# Intersection -> takes minimum of counts
print(f"c1 & c2 = {c1 & c2}")
# Union -> takes maximum of counts
print(f"c1 | c2 = {c1 | c2}")
Output:
c1 + c2 = Counter({'a': 5, 'b': 4, 'c': 3, 'd': 2})
c1 - c2 = Counter({'a': 3})
c1 & c2 = Counter({'b': 2, 'a': 1})
c1 | c2 = Counter({'a': 4, 'd': 4, 'c': 3, 'b': 2})
10 effectively rotating elements with 10 deque
deque
The object is A rotate()
The method that allows you to effectively rotate the elements. A positive argument rotates the elements from the right. A negative, to the left. It is much faster than a slice and re -adding lists or topus.
from collections import deque
d = deque((1, 2, 3, 4, 5))
print(f"Original deque: {d}")
# Rotate 2 steps to the right
d.rotate(2)
print(f"After rotating 2 to the right: {d}")
# Rotate 3 steps to the left
d.rotate(-3)
print(f"After rotating 3 to the left: {d}")
Output:
Original deque: deque((1, 2, 3, 4, 5))
After rotating 2 to the right: deque((4, 5, 1, 2, 3))
After rotating 3 to the left: deque((2, 3, 4, 5, 1))
Wrap
collections
The module is a killer collection of special, high -performance container data type in Azar. From counting items with Counter
To make efficient rows with deque
These tools can make your code clean, more efficient and more exciting. By familiarizing yourself with these amazing and powerful features, you can solve ordinary programming issues more beautiful and efficiently.
Matthew Mayo For,,,,,,,,,, for,, for,,,, for,,,, for,,, for,,,, for,,,, for,,,, for,,, for,,, for,,, for,,, for,,,, for,,, for,,, for,,,, for,,, for,,,, for,,, for,,, for,,,, for,,, for,,, for,,,, for,,, for,,,, for,,, for,,,, for,,, for,,,, for,,, for,,,, for,,,, for,,,, for,,,, for,,,, for,,,, for,,,, for,,, for,,, for,,, for,,, for,,,,, for,,,, for,,,, for,,,, for,, for,.@MattMayo13) Computer science is a graduate diploma in master’s degree and data mining. As the Managing Editor of Kdnuggets & StatologyAnd supporters in the editor Machine specializes in learningMatthew aims to make complex data science concepts accessible. Its professional interests include natural language processing, language models, machine learning algorithms, and the search for emerging AI. He is driven by a mission to democratic knowledge in the data science community. Matthew has been coding since the age of 6.