Q:

Python program to remove all tuples of length K

belongs to collection: Python Tuple Programs

0

Example:

tuple = ("python", "includehelp", 43, 54.23)

Removing all Tuples of Length K

In the program, we are given a list containing tuples of variable length. And we will be printing a list that will contain all tuples from the list except the ones with length k.

Example:

Input:
[(1, 4), (2), (4,5,6,8), (26), (3, 0, 1), (4)] k = 1

Output:
[(1, 4), (4, 5, 6, 8), (3, 0, 1)]

To eliminate all tuples of length k we need to traverse the list of tuples and then find the lengths of all the tuples and remove tuples from the list whose length is equal to k.

In Python programming language, there are multiple ways to perform a single task in different ways and it depends on the programmer and then the need of the software being developed that which one should be used.

All Answers

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

Method 1:

One method to remove the tuples is using the filter method which filters off every element which does not satisfy the entry condition.

In the filter method, we will pass the lambda function to iterate over all elements of the array and check if the length is equal to k or not. Elements with length K are not allowed to pass through the function. The list returned by the filter method is the required list.

Program:

# Python program to remove all tuple of length k

# Creating the list of tuples
tupleList = [(1, 4), (9, 4, 2), (4, 5, 6, 8), (2, 6, 8), (3, 0, 1), (4, 4, 1)]
K = 2
print("Initial List : " + str(tupleList))

# removing tuples of length k 
filteredList = list(filter(lambda tup : len(tup) != K, tupleList))

# Printing the filtered list 
print("List of tuples after removing tuple of length k : " + str(filteredList))

Output:

Initial List : [(1, 4), (9, 4, 2), (4, 5, 6, 8), (2, 6, 8), (3, 0, 1), (4, 4, 1)]
List of tuples after removing tuple of length k : [(9, 4, 2), (4, 5, 6, 8), (2, 6, 8), (3, 0, 1), (4, 4, 1)]

The same task can be performed using list comprehension that does iteration over the list and checking of elements with length other than k. And returns their values.

Program:

# Python program to remove all tuple of length k

# Creating the list of tuples
tupleList =  [(1, 4), (9, 4, 2), (4,5,6,8), (2, 6, 8), (3, 0, 1), (4, 4, 1)] 
K = 2
print("Initial List : " + str(tupleList))

# removing tuples of length k 
filteredList = [tup for tup in tupleList if len(tup) != K]

# Printing the filtered list 
print("List of tuples after removing tuple of length k : " + str(filteredList))

Output:

Initial List : [(1, 4), (9, 4, 2), (4, 5, 6, 8), (2, 6, 8), (3, 0, 1), (4, 4, 1)]
List of tuples after removing tuple of length k : [(9, 4, 2), (4, 5, 6, 8), (2, 6, 8), (3, 0, 1), (4, 4, 1)]

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 extract digits from tuple list... >>
<< Python program to create a list of tuples from giv...