Home »
Python »
Python Programs
Python program to remove all tuples of length K
Here, we have a list of tuples of variable length and we need to remove all the tuples of length k from the list in Python programming language.
By Shivang Yadav Last updated : December 12, 2023
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)
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.
For removing tuples of a specific length in python there are more than one way. Let's see some of them...
Method 1: Using filter() Method and Lambda Function
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)]
Method 2: Using List Comprehension
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)]
Python Tuple Programs »