Fast Efficient way to transfer an object to another list
Kushal Kumaran
kushal.kumaran+python at gmail.com
Fri Apr 30 23:13:17 EDT 2010
On Sat, May 1, 2010 at 7:46 AM, Jimbo <nilly16 at yahoo.com> wrote:
> 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]
Does your code work correctly? Modifying old_list while iterating
over it will not work the way you expect, unless you're careful. And
what is the point of temp_stock?
--
regards,
kushal
More information about the Python-list
mailing list