DEV Community 👩‍💻👨‍💻

Discussion on: Things that weren't so obvious when you started to program in Python

kmyokoyama profile image
Kazuki Yokoyama • Edited on

I'd add an one line conditional counting:

amounts = [2, 24, 88, 32, 1, 6, 88, 10, 15, 34]
greater_than_15 = sum(1 for amount in amounts if amount > 15)

Since this creates a generator and then sums over it, it avoids creating an intermediary copy of the items in the list that satisfy the condition.