Alvaro Silvino
Alvaro Silvino

Reputation: 9733

Python filter tuples with different sizes

I'm trying to filter this list of tuples:

listTuples = [(50,), (60,), (70,), (120,), (50, 50), (60, 50), (70, 50), (120, 50), (50, 50,60), (60, 50, 60), (70, 50, 60), (120, 50, 60), (50, 50, 60, 70), (60, 50, 60,70), (70, 50, 60, 70), (120, 50, 60, 70)]

the filter will check if the sum of the tuple is bigger then 100

How Can I do that in python using filter ?

Upvotes: 0

Views: 61

Answers (1)

Copperfield
Copperfield

Reputation: 8510

use a lambda function with the build-in sum

>>> listTuples = [(50,), (60,), (70,), (120,), (50, 50), (60, 50), (70, 50), (120, 50), (50, 50,60), (60, 50, 60), (70, 50, 60), (120, 50, 60), (50, 50, 60, 70), (60, 50, 60,70), (70, 50, 60, 70), (120, 50, 60, 70)]
>>> filter(lambda x:sum(x)>100,listTuples)
[(120,), (60, 50), (70, 50), (120, 50), (50, 50, 60), (60, 50, 60), (70, 50, 60), (120, 50, 60), (50, 50, 60, 70), (60, 50, 60, 70), (70, 50, 60, 70), (120, 50, 60, 70)]
>>> 

Upvotes: 1

Related Questions