Reinhold Birkenfeld <[EMAIL PROTECTED]> writes:

>> Quick solution:
>> 
>> for i in lst[:]
>> 
>> iterates over a copy.
>
> Addition: In Py2.4, I can't find a problem with
>
> for i in reversed(lst)
>
> Any objections?
>
> Reinhold

I just downloaded py2.4, and made a test using reversed.
it sure be no problem, I thought maybe the reversed holded a copy of list,
and eventually iterated through the copy.
but the truth is not as I thought so:

import sys

class Test:
    pass

lst = [Test(),Test(),Test()]

E1: for i in lst[:]:
E2: for i in reversed(lst):
    print sys.getrefcount(i)

###################
E1 outputs:
4
4
4

E2 outputs:
3
3
3

It looks that the reversed does not make a copy of list in contrast with lst[:].
so should we regard: reversed is faster than lst[:]?
I do not have any idea about why it is.

- skull
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to