Given this list of tuples:
l1=[(0, 90),
(1, 532, 17),
(2, 5080),
(3, 2516, 94)]
How could I extract all those tuples that have more than two elements? In this case, the result would be:
l2=[(1, 532, 17),(3, 2516, 94)]
Given this list of tuples:
l1=[(0, 90),
(1, 532, 17),
(2, 5080),
(3, 2516, 94)]
How could I extract all those tuples that have more than two elements? In this case, the result would be:
l2=[(1, 532, 17),(3, 2516, 94)]
Use a list comprehension and filter using len
:
l2 = [tup for tup in l1 if len(tup) > 2]
print(l2)
# [(1, 532, 17), (3, 2516, 94)]
filter it with list comprehension:
l1=[(0, 90),
(1, 532, 17),
(2, 5080),
(3, 2516, 94)]
l2 = [x for x in l1 if len(x)>2]
print(l2)
result:
[(1, 532, 17), (3, 2516, 94)]