- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python – Filter Tuples Product greater than K
When it is required to filter tuples product greater than K, a list comprehension is used.
Below is a demonstration of the same −
def tuples_product(index): my_result = 1 for element in index: my_result *= element return my_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) my_result = [index for index in my_list if tuples_product(index) > K] print("The result is :") print(my_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)]
A method named ‘tuples_product’ is defined that takes tuple as a parameter and returns the product of every element in the tuple as output.
Outside the method, a list is defined and displayed on the console.
The value for key is defined and is displayed on the console.
A list comprehension is used to iterate over the list, and for every element, the method is called.
The method’s result is compared to K.
If it is greater than K, it is added to a list and is assigned to a variable.
This is the output that is displayed on the console.
- Python – Remove Tuples with difference greater than K
- Python Indices of numbers greater than K
- Python – Remove characters greater than K
- Remove tuples from list of tuples if greater than n in Python
- Find smallest element greater than K in Python
- Python – Average of digit greater than K
- Python - Consecutive Ranges of K greater than N
- Python - Number of values greater than K in list
- Python - Get the Index of first element greater than K
- Python – Extract dictionaries with values sum greater than K
- Subarray Product Less Than K in C++
- Python – Filter Tuples with Integers
- Python – Filter unique valued tuples
- Python – Filter consecutive elements Tuples
- Python – Extract list with difference in extreme values greater than K