- 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 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 −
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))
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)]
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.
- Find top K frequent elements from a list of tuples in Python
- Python program to find Tuples with positive elements in a List of tuples
- 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
- Program to find elements from list which have occurred at least k times in Python
- Python program to sort a list of tuples by second Item
- Python program to convert elements in a list of Tuples to Float
- Combining tuples in list of tuples in Python
- Python program to Convert a elements in a list of Tuples to Float
- Python | Remove empty tuples from a list
- Filter Tuples by Kth element from List in Python
- Remove tuples from list of tuples if greater than n in Python
- Python program to sort a list of tuples alphabetically