The Cost of Dynamism (was Re: Pyhon 2.x or 3.x, which is faster?)
BartC
bc at freeuk.com
Thu Mar 24 10:04:53 EDT 2016
On 24/03/2016 13:50, Steven D'Aprano wrote:
> On Thu, 24 Mar 2016 02:24 pm, Chris Angelico wrote:
>
>
>> This is how you're currently evaluating Python. Instead of starting
>> with the most simple and obvious code
>
> One problem is that what counts as "simple and obvious" depends on what you
> are used to. Coming from a background of Pascal, iterating over a list like
> this:
>
> for i in range(len(mylist)):
> print mylist[i]
>
> was both simple and obvious. It took me years to break myself of that habit.
>
> Likewise clearing a list:
>
> for i in range(len(mylist)-1, -1, 0):
> del mylist[i]
That's wouldn't be I'd call clearing a list, more like destroying it
completely!
How would you actually clear a list by traversing it (ie. not just
building a new one)?
This doesn't work:
for x in L:
x=0
as each x only refers to the value in each element of L, not the element
itself (like the pass-by-reference problem).
I'd presumably have to do:
for i in range(len(L)):
L[i]=0
--
Bartc
More information about the Python-list
mailing list