-1

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)]
FaCoffee
  • 7,609
  • 28
  • 99
  • 174
  • 1
    It looks like you want us to write some code for you. While many users are willing to produce code for a coder in distress, they usually only help when the poster has already tried to solve the problem on their own. A good way to demonstrate this effort is to include the code you've written so far, example input (if there is any), the expected output, and the output you actually get (output, tracebacks, etc.). The more detail you provide, the more answers you are likely to receive. Check the [FAQ](http://stackoverflow.com/tour) and [How to Ask](http://stackoverflow.com/questions/how-to-ask). – TigerhawkT3 Feb 16 '17 at 22:33

2 Answers2

2

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)]
Moses Koledoye
  • 77,341
  • 8
  • 133
  • 139
1

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)]
Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219