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

Here, we have a list of tuples and we need to find all the tuples from the list which have all elements that are divisible by K in Python.
Submitted by Shivang Yadav, on July 31, 2021

Python programming language is a high-level and object-oriented programming language. Python is an easy to learn, powerful high-level programming language. It has a simple but effective approach to object-oriented programming.

Tuples in Python is a collection of items similar to list with the difference that it is ordered and immutable.

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.

Let's see them,

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)]

Python Tuple Programs »



Related Programs



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.