Python - Remove Tuples of Length K

Last Updated : 1 Oct, 2026

When working with a list of tuples, we may need to remove tuples having a specific number of elements. This can be done by checking the length of each tuple and keeping only those whose length is not equal to K.

Using List Comprehension

List comprehension provides a concise way to create a new list while excluding tuples whose length matches K. It is the simplest approach for this problem.

Python
test_list = [(1, 2), (3, 4, 5), (6,), (7, 8, 9), (10, 11)]
K = 2

res = [tup for tup in test_list if len(tup) != K]

print("Original list:", test_list)
print("After removing tuples of length", K, ":", res)

Output
Original list: [(1, 2), (3, 4, 5), (6,), (7, 8, 9), (10, 11)]
After removing tuples of length 2 : [(3, 4, 5), (6,), (7, 8, 9)]

Explanation:

  • Each tuple in test_list is checked using len().
  • Tuples having length equal to K are excluded.
  • Remaining tuples are stored in res.
  • Here, (1, 2) and (10, 11) are removed because both contain 2 elements.

Using filter()

This can also be used to retain only tuples whose length is different from K. It returns an iterator, so list() is used to obtain the final list.

Python
test_list = [(1, 2), (3, 4, 5), (6,), (7, 8, 9), (10, 11)]
K = 2

res = list(filter(lambda tup: len(tup) != K, test_list))

print("Original list:", test_list)
print("After removing tuples of length", K, ":", res)

Output
Original list: [(1, 2), (3, 4, 5), (6,), (7, 8, 9), (10, 11)]
After removing tuples of length 2 : [(3, 4, 5), (6,), (7, 8, 9)]

Explanation:

  • filter() examines each tuple in test_list.
  • The lambda expression returns True when its length is not equal to K.
  • Only tuples satisfying this condition are retained.
  • list() converts the resulting filter object into a list.

Using map() and lambda function

This approach combines filter() to exclude tuples of a given length and map() to return the remaining ones. The lambda function defines the condition based on tuple length using len().

Python
t1 = [(4, 5), (4, ), (8, 6, 7), (1, ), (3, 4, 6, 7)]
k = 1
n1 = list(map(lambda x: x, filter(lambda x: len(x) != k, t1)))
print(n1)

Output
[(4, 5), (8, 6, 7), (3, 4, 6, 7)]

Explanation:

  • filter() keeps only tuples whose length is not equal to k.
  • map() passes the filtered tuples unchanged.
  • list() converts the result into a list.
Comment