I came up with a IMHO nice piece of code while working with and getting to know Python.
incomplete_items = [
item.quantity_ordered > item.quantity_delivered for item in order.items
]
if any(incomplete_items):
do_something()
This feels clean and obvious. It might not be very efficient though. :/
has_incomplete_items = sum(
item.quantity_ordered > item.quantity_delivered for item in order.items
)
if has_incomplete_items:
do_something()
Doing it this way can be more efficient, since it can leverage generators and doesn’t need to go through the list again with
any
. But using
sum
over booleans feels hackish and non-obvious … 🙁