4 ms·
If it wasn't possible you could still do: sum(x > 3 for int(x) in iterable) And avoid a class of errors in other places.
by cma 8y ago
If it wasn't possible you could still do:
sum(x > 3 for int(x) in iterable)
And avoid a class of errors in other places.
- duckerude 8y agoThat code is invalid, because you're using "int(x)" as an assignment target. What would you expect it to do?
- cma 8y agosorry: sum(int(x) > 3 for x in iterable)
- duckerude 8y agoThat works better if you expect things in iterable that aren't ints, but it's still summing bools. int(x) > 3 returns a bool, so in the end it's still doing something similar to sum([True, False, False, True, True, False]) But if bools didn't implicitly behaves like ints, but could still be converted to ints, you could do this: sum(int(x > 3) for x in iterable)