- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
- Python Advanced Tutorial
- Python - Classes/Objects
- Python - Reg Expressions
- Python - CGI Programming
- Python - Database Access
- Python - Networking
- Python - Sending Email
- Python - Multithreading
- Python - XML Processing
- Python - GUI Programming
- Python - Further Extensions
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.
- Related Articles
- Python â€“ Remove Tuples with difference greater than K
- Remove tuples from list of tuples if greater than n in Python
- Python â€“ Remove characters greater than K
- Python Indices of numbers greater than K
- Python â€“ Average of digit greater than K
- Python â€“ Filter Tuples with Integers
- Python â€“ Filter unique valued tuples
- Python â€“ Filter consecutive elements Tuples
- Find smallest element greater than K in Python
- Python - Consecutive Ranges of K greater than N
- Python - Number of values greater than K in list
- Python â€“ Extract dictionaries with values sum greater than K
- Python - Get the Index of first element greater than K
- Python â€“ Trim tuples by K
- Python â€“ Extract list with difference in extreme values greater than K