Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python – Filter Tuples Product greater than K
When working with tuples in Python, you may need to filter tuples based on the product of their elements. This involves calculating the product of all elements in each tuple and keeping only those tuples where the product exceeds a threshold value K.
Using List Comprehension with Helper Function
The most readable approach is to create a helper function to calculate the product, then use list comprehension for filtering ?
def tuples_product(tuple_data):
result = 1
for element in tuple_data:
result *= element
return result
my_list = [(14, 25, 17), (2, 3, 5), (81, 42, 21), (6, 2, 1)]
print("The list is:")
print(my_list)
K = 15
print("The value of K is:")
print(K)
filtered_result = [tuple_data for tuple_data in my_list if tuples_product(tuple_data) > K]
print("The result is:")
print(filtered_result)
The list is: [(14, 25, 17), (2, 3, 5), (81, 42, 21), (6, 2, 1)] The value of K is: 15 The result is: [(14, 25, 17), (2, 3, 5), (81, 42, 21)]
Using math.prod() (Python 3.8+)
For Python 3.8 and later, you can use the built-in math.prod() function for a more concise solution ?
import math
my_list = [(14, 25, 17), (2, 3, 5), (81, 42, 21), (6, 2, 1)]
K = 15
filtered_result = [tuple_data for tuple_data in my_list if math.prod(tuple_data) > K]
print("Filtered tuples with product > K:")
print(filtered_result)
# Let's see the actual products
for tuple_data in my_list:
product = math.prod(tuple_data)
print(f"{tuple_data} ? product: {product}, > {K}? {product > K}")
Filtered tuples with product > K: [(14, 25, 17), (2, 3, 5), (81, 42, 21)] (14, 25, 17) ? product: 5950, > 15? True (2, 3, 5) ? product: 30, > 15? True (81, 42, 21) ? product: 71442, > 15? True (6, 2, 1) ? product: 12, > 15? False
Using Lambda Function
You can also use a lambda function with the filter() method for a functional programming approach ?
from functools import reduce
import operator
my_list = [(14, 25, 17), (2, 3, 5), (81, 42, 21), (6, 2, 1)]
K = 15
# Using lambda with reduce for product calculation
product_func = lambda t: reduce(operator.mul, t, 1)
filtered_result = list(filter(lambda t: product_func(t) > K, my_list))
print("Filtered tuples:")
print(filtered_result)
Filtered tuples: [(14, 25, 17), (2, 3, 5), (81, 42, 21)]
Comparison of Methods
| Method | Python Version | Readability | Performance |
|---|---|---|---|
| Helper Function + List Comprehension | All versions | High | Good |
| math.prod() | 3.8+ | Very High | Best |
| Lambda + filter() | All versions | Medium | Good |
Conclusion
Use math.prod() for the cleanest solution in Python 3.8+. For older versions, the helper function approach provides the best balance of readability and performance. List comprehension is generally preferred over filter() for better readability in Python.
