Signup/Sign In
PUBLISHED ON: AUGUST 11, 2021

Python Program To Remove Tuples of Length K

In this, tutorial we're given a list of tuples with varying lengths and the task is to remove tuples of length k.

Understand the Problem

To delete all tuples with a length of k, we must scan the list of tuples, discover the lengths of all the tuples, and then remove tuples from the list with a length of k. There are numerous ways to do a single operation in the Python programming language, and which one should be chosen depends on the programmer and then the requirement of the product being produced.

Let us have a look at the examples to understand the input-output format:

Input: list = [(1, 2), (3, ), (4, 5, 6), (7, ), (8, 9, 10, 11)] K=2

Output: [(3,), (4, 5, 6), (7,), (8, 9, 10, 11)]

Input: List = [(12, 13, 14), (14, 15), (16, 17), (18, 19)] K=2

Output: [(12, 13, 14)]

To execute this task, we can follow multiple approaches, some are discussed below:

  1. Using List comprehensions

  2. Using Filter, lambda, and len functions

We will discuss all these approaches in detail separately.

Approach 1: Using List comprehensions

This is the first method for doing this task. We will iterate for all elements in the loop and use conditions to complete the needed task of removing K length elements.

Algorithm

  1. A tuple list is created and presented on the console.
  2. On the console, the value of K is allocated and shown.
  3. The list comprehension method is used to get the length of each element in a tuple list.
  4. The list is allocated to a variable
  5. Print Out the result

Program to Remove Tuple

List comprehension, which iterates through the list and checks for entries with lengths other than "k", can be used to complete the task. And it gives them back their ideals.

List =  [(12, 13, 14), (14, 15), (16, 17), (18, 19)]
#  original list
print("The original list : " + str(List))
# initializing K
K = 2
result = [a for a in List if len(a) != K]
# printing result
print("Resultant list : " + str(result))


The original list : [(12, 13, 14), (14, 15), (16, 17), (18, 19)]
Resultant list : [(12, 13, 14)]

Approach 2: Using Filter, Lambda, and len functions

We'll use the lambda function in the filter method to loop over all items of the array and see if the length is equal to k or not. The function does not allow elements with a length of K to pass through. The needed list is the list returned by the filter function.

Algorithm

  1. Initializing List
  2. Print the original List
  3. Initializing value of k
  4. filter() filters non K length values
  5. Return the result
  6. Print the result

Program To Remove Tuple

We use the filter() and lambda functions in this program to extract only non-K length items with the len() function.

List =  [(1, 2), (3, ), (4, 5, 6), (7, ), (8, 9, 10, 11)]
K = 2
print("Orignal List : " + str(List))
# removing tuples of length k 
result = list(filter(lambda a : len(a) != K, List))
# Printing the resulting list 
print("Resulting Tuples : " + str(result))


Orignal List : [(1, 2), (3,), (4, 5, 6), (7,), (8, 9, 10, 11)]
Resulting Tuples : [(3,), (4, 5, 6), (7,), (8, 9, 10, 11)]

Conclusion

In this tutorial, we have seen two approaches to remove Tuples of Length K. The first approach is by using the list comprehension method which iterates through the list and checks for entries with lengths other than "k" and the second approach is by using filter lambda and len functions.



About the author:
Nikita Pandey is a talented author and expert in programming languages such as C, C++, and Java. Her writing is informative, engaging, and offers practical insights and tips for programmers at all levels.