Python program to find tuples which have all elements divisible by K from a list of tuples
Last Updated :
21 Mar, 2023
Given a list of tuples. The task is to extract all tuples which have all elements divisible by K.
Input : test_list = [(6, 24, 12), (60, 12, 6), (12, 18, 21)], K = 6
Output : [(6, 24, 12), (60, 12, 6)]
Explanation : Both tuples have all elements multiple of 6.
Input : test_list = [(6, 24, 12), (60, 10, 5), (12, 18, 21)], K = 5
Output : [(60, 10, 5)]
Explanation : Multiple of 5 tuples extracted.
Method #1 : Using list comprehension + all()
In this, we test for all elements to be K multiple using all() for filtering purpose, and list comprehension is used for the iteration of all tuples.
Python3
test_list = [( 6 , 24 , 12 ), ( 7 , 9 , 6 ), ( 12 , 18 , 21 )]
print ( "The original list is : " + str (test_list))
K = 6
res = [sub for sub in test_list if all (ele % K = = 0 for ele in sub)]
print ( "K Multiple elements tuples : " + str (res))
|
Output
The original list is : [(6, 24, 12), (7, 9, 6), (12, 18, 21)]
K Multiple elements tuples : [(6, 24, 12)]
Time Complexity: O(n*m)
Auxiliary Space: O(n)
Method #2 : Using filter() + lambda + all()
Step-by-step approach:
- In this, perform task of filtering using filter() + lambda, and logic provided by all().
- The program initializes a list called test_list containing three tuples, each with three integer values.
- The program prints the original list by concatenating a string with the string representation of test_list.
- The program initializes an integer variable K.
- The program uses the filter() function to filter the tuples in test_list based on the condition that all elements in the tuple are divisible by K.
- The program defines a lambda function that takes a sublist (tuple) as an argument and returns True if all elements in the sublist are divisible by K.
- The filter() function iterates through each tuple in test_list, applies the lambda function to each tuple, and keeps the tuples that satisfy the condition.
- The res variable stores the filtered tuples in a list.
- Prints the result by concatenating a string with the string representation of res.
Below is the implementation of the above approach:
Python3
test_list = [( 6 , 24 , 12 ), ( 7 , 9 , 6 ), ( 12 , 18 , 21 )]
print ( "The original list is : " + str (test_list))
K = 6
res = list ( filter ( lambda sub: all (ele % K = = 0 for ele in sub), test_list))
print ( "K Multiple elements tuples : " + str (res))
|
Output
The original list is : [(6, 24, 12), (7, 9, 6), (12, 18, 21)]
K Multiple elements tuples : [(6, 24, 12)]
Time Complexity: O(n*m)
Auxiliary Space: O(n)
Method #3 : Using list comprehension + not any()
In this, we test for all elements to be K multiple using not any () for filtering purpose, and list comprehension is used for iteration of all tuples.
Python3
test_list = [( 6 , 24 , 12 ), ( 7 , 9 , 6 ), ( 12 , 18 , 21 )]
K = 6
result = [tup for tup in test_list if not any (x % K ! = 0 for x in tup)]
print ( "The original list is : " + str (test_list))
print ( "K Multiple elements tuples : " + str (result))
|
Output
The original list is : [(6, 24, 12), (7, 9, 6), (12, 18, 21)]
K Multiple elements tuples : [(6, 24, 12)]
Time Complexity: O(n*m), where n is the number of tuples in the list, and m is the number of elements in each tuple.
Auxiliary Space: O(n), where n is the number of tuples in the list.
Method #4: Using for loops
Python3
test_list = [( 6 , 24 , 12 ), ( 7 , 9 , 6 ), ( 12 , 18 , 21 )]
print ( "The original list is : " + str (test_list))
K = 6
res = []
for i in test_list:
c = 0
for j in i:
if (j % K = = 0 ):
c + = 1
if (c = = len (i)):
res.append(i)
print ( "K Multiple elements tuples : " + str (res))
|
Output
The original list is : [(6, 24, 12), (7, 9, 6), (12, 18, 21)]
K Multiple elements tuples : [(6, 24, 12)]
Time Complexity: O(n*n), where n is the number of tuples in the list “test_list”.
Auxiliary Space: O(m), where m is the number of tuples in “res”.
Method #5: Using Recursive method.
Python3
def even_tuple(lst,K,newlst = [],start = 0 ):
if start = = len (lst):
return newlst
for i in lst[start]:
if i % K! = 0 :
return even_tuple(lst,K,newlst,start + 1 )
else :
newlst.append(lst[start])
return even_tuple(lst,K,newlst,start + 1 )
test_list = [( 6 , 24 , 12 ), ( 7 , 9 , 6 ), ( 12 , 18 , 21 )]
print ( "The original list is : " + str (test_list))
K = 6
res = even_tuple(test_list,K)
print ( "K Multiple elements tuples : " + str (res))
|
Output
The original list is : [(6, 24, 12), (7, 9, 6), (12, 18, 21)]
K Multiple elements tuples : [(6, 24, 12)]
Time Complexity: O(n*m), where n is length of the list and m is length of each tuple.
Auxiliary Space: O(n)
Method 6: Using the filter() function, without using lambda and all()
The implementation defines a function is_multiple_of_K() that checks if all elements in a tuple are multiples of K, and then applies the filter() function to keep only the tuples that satisfy this condition. Finally, it converts the filtered result into a list and prints it.
Python3
test_list = [( 6 , 24 , 12 ), ( 7 , 9 , 6 ), ( 12 , 18 , 21 )]
K = 6
def is_multiple_of_K(tup):
return all (ele % K = = 0 for ele in tup)
res = list ( filter (is_multiple_of_K, test_list))
print ( "The original list is : " + str (test_list))
print ( "K Multiple elements tuples : " + str (res))
|
Output
The original list is : [(6, 24, 12), (7, 9, 6), (12, 18, 21)]
K Multiple elements tuples : [(6, 24, 12)]
Time complexity: O(n), where n is the number of tuples in the test_list.
Auxiliary space: O(1), as the function only uses a constant amount of space to store the K value.
Method #7: Using numpy array and np.all()
Python3
import numpy as np
test_list = [( 6 , 24 , 12 ), ( 7 , 9 , 6 ), ( 12 , 18 , 21 )]
print ( "The original list is : " + str (test_list))
K = 6
arr = np.array(test_list)
res = arr[np. all (arr % K = = 0 , axis = 1 )]
print ( "K Multiple elements tuples : " + str (res))
|
Output:
K Multiple elements tuples : [[ 6 24 12]]
Time Complexity: O(nm), where n is the number of tuples in the input list, and m is the number of elements in each tuple.
Auxiliary Space: O(nm)
Similar Reads
Python program to find Tuples with positive elements in List of tuples
Given a list of tuples. The task is to get all the tuples that have all positive elements. Examples: Input : test_list = [(4, 5, 9), (-3, 2, 3), (-3, 5, 6), (4, -6)] Output : [(4, 5, 9)] Explanation : Extracted tuples with all positive elements. Input : test_list = [(-4, 5, 9), (-3, 2, 3), (-3, 5, 6
10 min read
Python program to create a list of tuples from given list having number and its cube in each tuple
We are given a list of numbers and our task is to create a list of tuples where each tuple contains a number and its cube. For example, if the input is [1, 2, 3], the output should be [(1, 1), (2, 8), (3, 27)]. Using List ComprehensionList comprehension is one of the most efficient ways to achieve t
3 min read
Removing Tuples from a List by First Element Value - Python
In this problem we need to delete tuples based on a specific condition related to their first element. For example: We are given the list data = [("GeeksforGeeks", "Python", 1000), ("CodingForAll", "Java", 1200)] and we need to remove all tuples where the first element is "GeeksforGeeks", the desire
3 min read
Python Program to remove elements that are less than K difference away in a list
Given a list, perform removal of those elements whose difference is less than K from its previous element. Input : test_list = [3, 19, 5, 8, 10, 13], K = 4 Output : [3, 8, 13, 19] Explanation : 5 - 3 = 2, 2<4, hence 5 is removed, similarly, 10 - 8 is 2, less than K. Input : test_list = [15, 7, 20
3 min read
Generating a "Set Of Tuples" from A "List of Tuples" - Python
We are given a list of tuples and we need to extract only the unique tuples while removing any duplicates. This is useful in scenarios where you want to work with distinct elements from the list. For example:We are given this a list of tuples as [(1, 2), (3, 4), (1, 2), (5, 6)] then the output will
3 min read
Getting an Element from Tuple of Tuples in Python
Tuples in Python are versatile data structures that allow you to store and organize collections of elements. When dealing with tuples of tuples, accessing specific elements becomes essential for effective data manipulation. In this article, we'll explore some different methods to retrieve elements f
2 min read
Python Program to print all the numbers divisible by 3 and 5 for a given number
Given the integer N, the task is to print all the numbers less than N, which are divisible by 3 and 5.Examples : Input : 50Output : 0 15 30 45 Input : 100Output : 0 15 30 45 60 75 90 Approach: For example, let's take N = 20 as a limit, then the program should print all numbers less than 20 which are
2 min read
Python Program to check whether it is possible to make a divisible by 3 number using all digits in an array
Given an array of integers, the task is to find whether it's possible to construct an integer using all the digits of these numbers such that it would be divisible by 3. If it is possible then print "Yes" and if not print "No". Examples: Input : arr[] = {40, 50, 90} Output : Yes We can construct a n
2 min read
Python Program to Find Numbers Divisible by Another Number
We are given a list of numbers and a number. We have to find all the numbers in the list that are divisible by the given single number. Examples: Input: list=[8, 14, 21, 36, 43], num=3Output: 21, 36, 57Input: list=[2, 17, 25, 31, 48, 55], num=5Output: 25, 55In this article, we will discuss the diffe
3 min read
Python - Sort Tuple List by Nth Element of Tuple
We are given list of tuple we need to sort tuple by Nth element of each tuple. For example d = [(1, 5), (3, 2), (2, 8), (4, 1)] and k=1 we need to sort by 1st element of each tuple so that output for given list should be [(4, 1), (3, 2), (1, 5), (2, 8)] Using sorted() with lambdasorted() function wi
3 min read