You can use
itertools.groupby
to group the items by order number.Don't use
is
to compare numbers.is
checks object identity and the same number can be represented by different objects:>>> a=1234 >>> b=1234 >>> a is b False >>> a=12 >>> b=12 >>> a is b True
Checking
if len(orders) > 0
before thefor
loop is redundant. If the list is empty the loop won't run even once.
Revised code:
from itertools import groupby
from operator import itemgetter
# Fetch my orders
orders = repo.get_orders_for(self._customer[0])
for order_number, items in groupby(orders, key=itemgetter(0)):
print("Order #{}".format(order_number))
for _, quantity, pid in items:
print(" Product #{}, ordered x{}".format(pid, quantity))