Python Practice › Tuple Programs › Program 26

Remove tuples of a given length

EasyTuple Programs · 26 of 70

Write a Python program to remove tuples of length i from a list.

Advertisement
Input
[(2, 5, 7), (3, 4), (8, 9, 0, 5)], i = 2
Output
[(2, 5, 7), (8, 9, 0, 5)]

Approach

  1. Keep tuples whose length is not i.

Solution

Python
data = [(2, 5, 7), (3, 4), (8, 9, 0, 5)]i = 2print([t for t in data if len(t) != i])
Output
[(2, 5, 7), (8, 9, 0, 5)]
Advertisement

Learn the concept

New to this topic? Read the lesson, then come back.

More tuple programs

← All 70 Tuple Programs