Connect with us

Jobs & Careers

Python functools & itertools: 7 Super Handy Tools for Smarter Code

Published

on



Image by Author | Ideogram

 

Python’s standard library has several utilities that can transform your code from clunky and verbose to elegant and efficient. Among these, the functools and itertools modules often come in super handy for non-trivial tasks.

Today, we’ll look at seven essential tools — functions and decorators — from these modules that’ll make your Python code better.

Let’s get started.

🔗 Link to the code on GitHub

 

1. functools.lru_cache

 
You can use the @lru_cache decorator to cache function results, and to avoid repeating expensive operations.

Here’s an example:

from functools import lru_cache

@lru_cache(maxsize=128)
def fetch_user_data(user_id):
    # Expensive database call
    return database.get_user(user_id)

# First call hits database, subsequent calls use cache
user = fetch_user_data(123)  # Database call
user = fetch_user_data(123)  # Returns cached result

 

How it works: The @lru_cache decorator stores results in memory. When fetch_user_data(123) is called again, it returns the cached result instead of hitting the database. maxsize=128 keeps the 128 most recent results.

 

2. itertools.chain

 
To process multiple iterables as one continuous stream, you can use chain.from_iterable() from the itertools module.

Let’s take an example:

from itertools import chain

# Process multiple log files as one stream
error_logs = ['app.log', 'db.log', 'api.log']
all_lines = chain.from_iterable(open(f) for f in error_logs)

error_count = sum(1 for line in all_lines if 'ERROR' in line)

 

How it works: chain.from_iterable() takes multiple iterables and creates one continuous stream. It reads one line at a time.

 

3. functools.partial

 
Partial functions in Python are super helpful when you need to create specialized versions of functions. Meaning you’d like to create versions of the function with some arguments already set using partial from the functools module.

Here’s an example of a partial function:

from functools import partial
import logging

def log_event(level, component, message):
    logging.log(level, f"[{component}] {message}")

# Create specialized loggers
auth_error = partial(log_event, logging.ERROR, 'AUTH')
db_info = partial(log_event, logging.INFO, 'DATABASE')

# Clean usage
auth_error("Login failed for user")
db_info("Connection established")

 

How it works: partial creates a new function with some arguments pre-filled. In the example, auth_error is essentially log_event with level and component already set, so you only need to provide the message.

 

4. itertools.combinations

 
When you need to generate all possible combinations of items for testing or optimization, you can use combinations from the itertools module.

Consider the following example:

from itertools import combinations

features = ['cache', 'compression', 'cdn']

# Test all pairs of features
for combo in combinations(features, 2):
    performance = test_feature_combo(combo)
    print(f"{combo}: {performance}ms")

 

How it works: combinations(features, 2) generates all possible pairs from the list. It creates combinations on-demand without storing them all in memory, making it efficient for large datasets.

 

5. functools.singledispatch

 
The @singledispatch decorator from the functools module can help you make functions that act differently based on input type.

Look at the following code snippet:

from functools import singledispatch
from datetime import datetime

@singledispatch
def format_data(value):
    return str(value)  # Default

@format_data.register(datetime)
def _(value):
    return value.strftime("%Y-%m-%d")

@format_data.register(list)
def _(value):
    return ", ".join(str(item) for item in value)

# Automatically picks the right formatter
print(format_data(datetime.now()))  # this outputs "2025-06-27"
print(format_data([1, 2, 3]))       # this outputs "1, 2, 3"

 

How it works: Python checks the type of the first argument and calls the appropriate registered function. However, it uses the default @singledispatch function if no specific handler exists.

 

6. itertools.groupby

 
You can group consecutive elements that share the same property using the groupby function from itertools.

Consider this example:

from itertools import groupby

transactions = [
    {'type': 'credit', 'amount': 100},
    {'type': 'credit', 'amount': 50},
    {'type': 'debit', 'amount': 75},
    {'type': 'debit', 'amount': 25}
]

# Group by transaction type
for trans_type, group in groupby(transactions, key=lambda x: x['type']):
    total = sum(item['amount'] for item in group)
    print(f"{trans_type}: ${total}")

 

How it works: groupby groups consecutive items with the same key. It returns pairs of (key, group_iterator). Important: it only groups adjacent items, so sort your data first if needed.

 

7. functools.reduce

 
You can use the reduce function from the functools module to apply a function cumulatively to all elements in an iterable to get a single value.

Take the following example:

from functools import reduce

# Calculate compound interest
monthly_rates = [1.01, 1.02, 0.99, 1.015]  # Monthly growth rates

final_amount = reduce(lambda total, rate: total * rate, monthly_rates, 1000)
print(f"Final amount: ${final_amount:.2f}")

 

How it works: reduce takes a function and applies it step by step: first to the initial value (1000) and the first rate, then to that result and the second rate, and so on. It works well for operations that build up state.

 

Wrapping Up

 
To sum up, we’ve seen how you can use:

  • @lru_cache when you have functions that are called often with the same arguments
  • itertools.chain when you need to process multiple data sources as one continuous stream
  • functools.partial to create specialized versions of generic functions
  • itertools.combinations for systematic exploration of possibilities
  • @singledispatch when you need type-based function behavior
  • groupby for efficient consecutive grouping operations
  • reduce for complex aggregations that build up state

The next time you find yourself writing verbose loops or repetitive code, pause and consider whether one of these might provide a more elegant solution.

These are just a handful of tools I find helpful. There are many more if you take a closer look at the Python standard library. So yeah, happy exploring!
 
 

Bala Priya C is a developer and technical writer from India. She likes working at the intersection of math, programming, data science, and content creation. Her areas of interest and expertise include DevOps, data science, and natural language processing. She enjoys reading, writing, coding, and coffee! Currently, she’s working on learning and sharing her knowledge with the developer community by authoring tutorials, how-to guides, opinion pieces, and more. Bala also creates engaging resource overviews and coding tutorials.





Source link

Continue Reading
Click to comment

Leave a Reply

Your email address will not be published. Required fields are marked *

Jobs & Careers

HCLSoftware Launches Domino 14.5 With Focus on Data Privacy and Sovereign AI

Published

on


HCLSoftware, a global enterprise software leader, launched HCL Domino 14.5 on July 7 as a major upgrade, specifically targeting governments and organisations operating in regulated sectors that are concerned about data privacy and digital independence.

A key feature of the new release is Domino IQ, a sovereign AI extension built into the Domino platform. This new tool gives organisations full control over their AI models and data, helping them comply with regulations such as the European AI Act.

 It also removes dependence on foreign cloud services, making it easier for public sector bodies and banks to protect sensitive information.

“The importance of data sovereignty and avoiding unnecessary foreign government influence extends beyond SaaS solutions and AI. Specifically for collaboration – the sensitive data within email, chat, video recordings and documents. With the launch of Domino+ 14.5, HCLSoftware is helping over 200+ government agencies safeguard their sensitive data,” said Richard Jefts, executive vice president and general manager at HCLSoftware

The updated Domino+ collaboration suite now includes enhanced features for secure messaging, meetings, and file sharing. These tools are ready to deploy and meet the needs of organisations that handle highly confidential data.

The platform is supported by IONOS, a leading European cloud provider. Achim Weiss, CEO of IONOS, added, “Today, more than ever, true digital sovereignty is the key to Europe’s digital future. That’s why at IONOS we are proud to provide the sovereign cloud infrastructure for HCL’s sovereign collaboration solutions.”

Other key updates in Domino 14.5 include achieving BSI certification for information security, the integration of security event and incident management (SEIM) tools to enhance threat detection and response, and full compliance with the European Accessibility Act, ensuring that all web-based user experiences are inclusive and accessible to everyone.

With the launch of Domino 14.5, HCLSoftware is aiming to be a trusted technology partner for public sector and highly regulated organisations seeking control, security, and compliance in their digital operations.



Source link

Continue Reading

Jobs & Careers

Mitsubishi Electric Invests in AI-Assisted PLM Systems Startup ‘Things’

Published

on


Mitsubishi Electric Corporation announced on July 7 that its ME Innovation Fund has invested in Things, a Japan-based startup that develops and provides AI-assisted product lifecycle management (PLM) systems for the manufacturing industry. 

This startup specialises in comprehensive document management, covering everything from product planning and development to disposal. According to the company, this marks the 12th investment made by Mitsubishi’s fund to date.

Through this investment, Mitsubishi Electric aims to combine its extensive manufacturing and control expertise with Things’ generative AI technology. The goal is to accelerate the development of digital transformation (DX) solutions that tackle various challenges facing the manufacturing industry.

In recent years, Japan’s manufacturing sector has encountered several challenges, including labour shortages and the ageing of skilled technicians, which hinder the transfer of expertise. In response, DX initiatives, such as the implementation of PLM and other digital systems, have progressed rapidly. However, these initiatives have faced challenges related to development time, cost, usability, and scalability.

Komi Matsubara, an executive officer at Mitsubishi Electric Corporation, stated, “Through our collaboration with Things, we expect to generate new value by integrating our manufacturing expertise with Things’ generative AI technology. We aim to leverage this initiative to enhance the overall competitiveness of the Mitsubishi Electric group.”

Things launched its ‘PRISM’ PLM system in May 2023, utilising generative AI to improve the structure and usage of information in manufacturing. PRISM offers significant cost and scalability advantages, enhancing user interfaces and experiences while effectively implementing proofs of concept across a wide range of companies.

Atsuya Suzuki, CEO of Things, said, “We are pleased to establish a partnership with Mitsubishi Electric through the ME Innovation Fund. By combining our technology with Mitsubishi Electric’s expertise in manufacturing and control, we aim to accelerate the global implementation of pioneering DX solutions for manufacturing.”



Source link

Continue Reading

Jobs & Careers

AI to Track Facial Expressions to Detect PTSD Symptoms in Children

Published

on


A research team from the University of South Florida (USF) has developed an AI system that can identify post-traumatic stress disorder (PTSD) in children.

The project addresses a longstanding clinical dilemma: diagnosing PTSD in children who may not have the emotional vocabulary, cognitive development or comfort to articulate their distress. Traditional methods such as subjective interviews and self-reported questionnaires often fall short. This is where AI steps in.

“Even when they weren’t saying much, you could see what they were going through on their faces,” Alison Salloum, professor at the USF School of Social Work, reportedly said. Her observations during trauma interviews laid the foundation for collaboration with Shaun Canavan, an expert in facial analysis at USF’s Bellini College of Artificial Intelligence, Cybersecurity, and Computing.

The study introduces a privacy-first, context-aware classification model that analyses subtle facial muscle movements. However, instead of using raw footage, the system extracts non-identifiable metrics such as eye gaze, mouth curvature, and head position, ensuring ethical boundaries are respected when working with vulnerable populations. 

“We don’t use raw video. We completely get rid of subject identification and only keep data about facial movement,” Canavan reportedly emphasised. The AI also accounts for conversational context, whether a child is speaking to a parent or a therapist, which significantly influences emotional expressivity.

Across 18 therapy sessions, with over 100 minutes of footage per child and approximately 185,000 frames each, the AI identified consistent facial expression patterns in children diagnosed with PTSD. Notably, children were more expressive with clinicians than with parents; a finding that aligns with psychological literature suggesting shame or emotional avoidance often inhibits open communication at home.

While still in its early stages, the tool is not being pitched as a replacement for therapists. Instead, it’s designed as a clinical augmentation, a second set of ‘digital’ eyes that can pick up on emotional signals even trained professionals might miss in real time.

“Data like this is incredibly rare for AI systems,” Canavan added. “That’s what makes this so promising. We now have an ethically sound, objective way to support mental health assessments.”

If validated on a larger scale, the system could transform mental health diagnostics for children—especially for pre-verbal or very young patients—by turning non-verbal cues into actionable insights. 



Source link

Continue Reading

Trending