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

spir denis.spir at free.fr
Sat Nov 1 23:18:39 CET 2008


Sander Sweers a écrit :
> Hi,
>
> What is the better way to process data in a list? Make the changes in
> place, for example
>
> somelist = [1,2,3,4]
>
> for x in range(len(somelist)):
>     somelist[x] = somelist[x] + 1
>
> Or would making a new list like
>
> somelist = [1,2,3,4]
> newlist = []
>
> for x in somelist:
>     newlist.append(x + 1)
>
> Or is there another way of doing this kind of things. Pointers to
> online resources are most welcome :-)
>   
Look for "list comprehension in the tutorial chap 5.1.4, or better Dive 
into Python chap 3.6:

/result/ = *[*/expression/ *for* /item1/ *in* /sequence/]
(result is a new list)

Denis

> Thx
> Sander
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>
>   




More information about the Tutor mailing list