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 iteration of all tuples.
Python3
# Python3 code to demonstrate working of# K Multiple Elements Tuples# Using list comprehension + all() # initializing listtest_list = [(6, 24, 12), (7, 9, 6), (12, 18, 21)] # printing original listprint("The original list is : " + str(test_list)) # initializing KK = 6 # all() used to filter elementsres = [sub for sub in test_list if all(ele % K == 0 for ele in sub)] # printing resultprint("K Multiple elements tuples : " + str(res)) |
The original list is : [(6, 24, 12), (7, 9, 6), (12, 18, 21)] K Multiple elements tuples : [(6, 24, 12)]
Method #2 : Using filter() + lambda + all()
In this, we perform task of filtering using filter() + lambda, and logic provided by all(), as in above method.
Python3
# Python3 code to demonstrate working of# K Multiple Elements Tuples# Using filter() + lambda + all() # initializing listtest_list = [(6, 24, 12), (7, 9, 6), (12, 18, 21)] # printing original listprint("The original list is : " + str(test_list)) # initializing KK = 6 # filter() + lambda for filter operationres = list(filter(lambda sub: all(ele % K == 0 for ele in sub), test_list)) # printing resultprint("K Multiple elements tuples : " + str(res)) |
The original list is : [(6, 24, 12), (7, 9, 6), (12, 18, 21)] K Multiple elements tuples : [(6, 24, 12)]
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course


