
- 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 program to find tuples which have all elements divisible by K from a list of tuples
When it is required to find tuples that have elements that are divisible by a specific element ‘K’, the list comprehension can be used.
Below is a demonstration of the same −
Example
my_list = [(45, 90, 135), (71, 92, 26), (2, 67, 98)] print("The list is : ") print(my_list) K = 45 print("The value of K has been initialized to ") print(K) my_result = [sub for sub in my_list if all(ele % K == 0 for ele in sub)] print("Elements divisible by K are : " + str(my_result))
Output
The list is : [(45, 90, 135), (71, 92, 26), (2, 67, 98)] The value of K has been initialized to 45 Elements divisible by K are: [(45, 90, 135)]
Explanation
A list of tuple is defined, and is displayed on the console.
The value of K is defined, and is displayed on the console.
The list comprehension is used to iterate through the elements.
Every element in the list of tuple is checked to see if it is divisible by K.
If it is divisible by K, it is converted to a list element, and stored in a variable.
This is displayed as output on the console.
- Related Articles
- Python program to find Tuples with positive elements in a List of tuples
- Find top K frequent elements from a list of tuples in Python
- Python program to find Tuples with positive elements in List of tuples
- Remove duplicate tuples from list of tuples in Python
- Python – Trim tuples by K
- Find the tuples containing the given element from a list of tuples in Python
- Python program to convert elements in a list of Tuples to Float
- Python program to sort a list of tuples by second Item
- Python program to Convert a elements in a list of Tuples to Float
- Python – Filter all uppercase characters from given list of tuples
- Python program to sort a list of tuples alphabetically
- Combining tuples in list of tuples in Python
- Program to find elements from list which have occurred at least k times in Python
- Remove tuples from list of tuples if greater than n in Python
- Extract tuples having K digit elements in Python

Advertisements