Fast Efficient way to transfer an object to another list

Jimbo nilly16 at yahoo.com
Fri Apr 30 22:16:04 EDT 2010


Hello I have a relatively simple thing to do; move an object from one
to list into another. But I think my solution maybe inefficient &
slow. Is there a faster better way to move my stock object from one
list to another? (IE, without having to use a dictionary instead of a
list or is that my only solution?)

[code]
class stock:

    code = "NULL"
    price = 0


stock_list1 = []
stock_list2 = []

def transfer_stock(stock_code, old_list, new_list):
    """ Transfer a stock from one list to another """
    # is there a more efficient & faster way to

    index = 0

    for stock in old_list:

        temp_stock = stock

        if temp_stock.code == stock_code:
            new_list.append(temp_stock)
            del old_list[index]
        index += 1

    return new_list[/code]



More information about the Python-list mailing list