from collections import deque ready=deque([("A",2),("B",1)]) order=[] while ready: name,left=ready.popleft() order.append(name) if left>1: ready.append((name,left-1)) print(" ".join(order))