insertion sorts...

A.T.Hofkamp hat at
Wed Jun 25 12:37:43 CEST 2008

On 2008-06-25, python_newbie <serbulentu at> wrote:
> On 24 Haziran, 04:33, Terry Reedy <tjre... at> wrote:
> Thanks for all answers. At the end i ve only one point. If a decide to
> copy list to iterate when will i have to do this ? Before the
> iteration ? And then iterate through one list and change value of the
> other ?

Before starting the iteration would be a good point....

I usually do in such cases:

for x in mylist[:]:

making a copy just before the for loop starts.

Lately, I have started avoiding in-place modification of lists. Instead, I
construct a new list from scratch inside the for-loop, and replace the old list
with the newly constructed list afterwards like:

new_list = []
for x in mylist:

mylist = new_list

by appending a different value than the original or by not appending, you can
influence the contents of the new list.

I find this solution nicer than in-place modification of an existing list.


More information about the Python-list mailing list