Python Practice › Tuple Programs › Program 26
Remove tuples of a given length
Write a Python program to remove tuples of length i from a list.
Input
[(2, 5, 7), (3, 4), (8, 9, 0, 5)], i = 2
Output
[(2, 5, 7), (8, 9, 0, 5)]
Approach
- Keep tuples whose length is not i.
Solution
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)]
Learn the concept
New to this topic? Read the lesson, then come back.