[Tutor] Manipulate list in place or append to a new list

Sander Sweers sander.sweers at gmail.com
Sun Nov 2 14:13:54 CET 2008


On Sun, Nov 2, 2008 at 13:32, Kent Johnson <kent37 at tds.net> wrote:
> Use a list comprehension:
> somelist = [ x+1 for x in somelist ]

Got it.

> Note that this creates a new list, replacing the one that was in
> somelist. If you need to actually modify somelist in place (rare) then
> use somelist[:] =  [ x+1 for x in somelist ]
>
> which creates a new list, then replaces the *contents* of somelist
> with the contents of the new list.

In what (rare) situation would you use this?

> http://personalpages.tds.net/~kent37/kk/00012.html

This page helped. Especially the link to the 'Reset your brain'
article is very interesting.

Thanks
Sander


More information about the Tutor mailing list