[perl-python] problem: reducing comparison (correction)

Xah Lee xah at xahlee.org
Wed Feb 16 16:55:02 EST 2005


Xah Lee wrote:
> In imperative languages such as Perl and Python and Java, in general
it
> is not safe to delete elements when looping thru a list-like entity.
> (it screws up the iteration) One must make a copy first, and work
with
> the copy.

Correction:
When looping thru a list-like entity and delete elements in the vary
list, there's a question whether it will change the iteration. (For
example, if one loops thru 1 to 9, and deleted 8 while at 2, should the
loop still do 8?) For some languages and or list entities, the answer
may be yes or no. However, in imperative languages such as Perl and
Python and Java, often this is not allowed by the language, partially
as a protection to safeguard and assume programers as ignoramuses, but
partially because the internal issues of these languages can't handle
it.

The work around in these languages is always to make a copy of the
list-entity, and work with the copy.

 Xah
 xah at xahlee.org
 http://xahlee.org/PageTwo_dir/more.html




More information about the Python-list mailing list