Example:
tuple = ("python", "includehelp", 43, 54.23)
List is a sequence data type. It is mutable as its values in the list can be modified. It is a collection of ordered set of values enclosed in square brackets [].
Example:
list = [3 ,1, 5, 7]
Finding tuples from list which have all elements divisible by K
We have a list of tuples and we need to find all the tuples from the list of tuples that have all elements of the tuple divisible by the given value k.
Input:
[(32, 5, 7), (12, 6, 4), (16, 9), (2, 8)], K = 2
Output:
[(12, 6, 4), (2, 8)]
We will check all elements of each tuple from the list and check for its divisibility by k. And filter all tuples with elements divisible by k. And for performing the task we have multiple methods.
Method 1:
A method to find the tuples, we will be using the filter() method and using the lambda function to perform the operation whether the element is divisible by k or not on all elements of the tuple.
Program:
Output:
Method 2:
Another approach to perform the task is by using the list comprehension to perform the filtering task.
Program:
Output:
need an explanation for this answer? contact us directly to get an explanation for this answer