Q:

Python program to find tuples from list which have all elements divisible by K

belongs to collection: Python Tuple Programs

0

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.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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:

# Python program to find tuples from list 
# which have all elements divisible by K

# Creating and Printing tuple list 
tupList = [(32, 5, 7), (12, 6, 4), (16, 9), (2, 8)]
print("Initial List : " + str(tupList))
K = 2

# Filtering tuples with values divisible by K
selectedTupList = list(filter(lambda tup: all(value % K == 0 for value in tup), tupList))

# Printing the tuple List with all elements divisible by K
print("List of Tuples which have all elements divisible by k : " + str(selectedTupList))

Output:

Initial List : [(32, 5, 7), (12, 6, 4), (16, 9), (2, 8)]
List of Tuples which have all elements divisible by k : [(12, 6, 4), (2, 8)]

Method 2:

Another approach to perform the task is by using the list comprehension to perform the filtering task.

Program:

# Python program to find tuples form list 
# which have all elements divisible by K

# Creating and Printing tuple list 
tupList = [(32, 5, 7), (12, 6, 4), (16, 9), (2, 8)]
print("Initial List : " + str(tupList))
K = 2

# Filtering tuples with values divisible by K
selectedTupList = [tup for tup in tupList if all(values % K == 0 for values in tup)]
  
# Printing the tuple List with all elements divisible by K
print("List of Tuples which have all elements divisible by k : " + str(selectedTupList))

Output:

Initial List : [(32, 5, 7), (12, 6, 4), (16, 9), (2, 8)]
List of Tuples which have all elements divisible by k : [(12, 6, 4), (2, 8)]

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Python Tuple Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Python program to remove tuples from the list havi... >>
<< Python program to extract tuples having K digit el...