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)
Please Login to comment...