2 for y in x) Or, as suggested by @Jochen, to guard against non-conventional nth-party classe..." />

Use lambda expression to count the elements that I'm interested in Python?

Note: "more than" is is not a valid operator Try sum(y > 2 for y in x) Or, as suggested by @Jochen, to guard against non-conventional nth-party classes, use this: sum(1 for y in x if y > 2).

Note: "more than" is > ... => is not a valid operator. Try sum(y > 2 for y in x) Or, as suggested by @Jochen, to guard against non-conventional nth-party classes, use this: sum(1 for y in x if y > 2).

Summing booleans. Nice. +1 for demonstrating that lambda is not necessary.

– Johnsyweb Sep 26 at 0:22 4 or sum(1 for y in x if y > 2) – Jochen Ritzel Sep 26 at 0:42 @TokenMacGuy: or it could be decimal, or rational, or some weird DIY class; whatever y is, surely y > 2 should not run amok. I don't see the point of float(y). Yes, some badass class could define comparison operators that return neither True nor 1 for truthy results -- in which case, the suggestion of @Jochen Thanks!

Avoids the explicit bool() – John Machin Sep 26 at 0:59 sum(1 for y in x if y > 2) is IMHO the best and most Pythonic answer. – steveha Sep 26 at 1:14.

You can try any of the following len(y for y in x if y > 2) or len(filter(lambda y: y > 2, x)) or the nicer sum( y > 2 for y in x ).

OP wanted "more than two", not "more than or equal to two". – John Machin Sep 26 at 0:32.

No, that won't work, this will though: len(y for y in x if y > 2).

AttributeError: 'list' object has no attribute 'filter' – prosseek Sep 26 at 0:29 OP wanted "more than two", not "more than or equal to two". – John Machin Sep 26 at 0:31 Oh sorry, what was I thinking? Hahaha I fixed it.

– sunjay03 Sep 26 at 0:32 1 Hahaha it's still ugly; it creates a temporary list needlessly. – John Machin Sep 26 at 0:41 -1 Do you test anything before posting? TypeError: object of type 'generator' has no len() – John Machin Sep 26 at 1:06.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions