'Moving item in lists with functions

My code runs but I'm expecting my orders to follow 3,2,1. To my knowledge pop() takes the last entry and returns it. So on my last call move_to_old_orders(made_orders) it returns in the list 1,2,3. If you look in the output it goes from 3,2,1 / 3,2,1 / 1, 2, 3. The print statements at the end are for me to verify the list is empty and has moved.

Code:

unmade_orders = ['order 1' , 'order 2', 'order 3']
made_orders = []
old_orders = []

def make_orders(unmade_orders, made_orders):
    ''' To make an order '''
    while unmade_orders:
        current_order = unmade_orders.pop()
        print("Order Processing: " + current_order.title() + ".")

        made_orders.append(current_order)

def print_orders(made_orders):
    ''' To print an order '''
    for made_order in made_orders:
        print("Order Processed: " + made_order.title() + ".")

make_orders(unmade_orders, made_orders)
print_orders(made_orders)

def move_to_old_orders(made_orders):
    while made_orders:
        current_order_1 = made_orders.pop()
        print("Moving Order To Old Orders: " + current_order_1.title() + ".")

        old_orders.append(current_order_1)

move_to_old_orders(made_orders)

print(unmade_orders)
print(made_orders)
print(old_orders)

Output:

Order Processing: Order 3.
Order Processing: Order 2.
Order Processing: Order 1.
Order Processed: Order 3.
Order Processed: Order 2.
Order Processed: Order 1.
Moving Order To Old Orders: Order 1.
Moving Order To Old Orders: Order 2.
Moving Order To Old Orders: Order 3.


Solution 1:[1]

Your function make_orders()
is appending the list make_orders = [] in this make_orders = ["order 3","order 2","order 1"] kind of format as it should be,
'cause it's the nature of append()

That's why when you call the function move_to_old_orders()
It starts from the last element which is order 1

That's why you are getting the output of 1 2 3

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Shobhit Thakur