It also seems fair to do the following (if the modified list is to be used
more than once - to avoid building the modified list more than once)?

array = [item*2 for item in array] # instead of >>>> for item in array: item
*= 2

Regards,
Trilok


-----Original Message-----
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf
Of Noufal Ibrahim
Sent: Monday, August 20, 2007 8:29 PM
To: János Juhász
Cc: tutor@python.org
Subject: Re: [Tutor] iterate list items as lvalue

János Juhász wrote:
> Dear Tutors!
>
> I know a python list is a mutable object.
>>>> array = [1,2,3,4,5]
>
> So I can modify any item in it.
>>>> for index in range(len(array)): array[index] *= 2
> ...
>>>> array
> [2, 4, 6, 8, 10]
>
> So I typed this:
>>>> for item in array: item *= 2
> ...
>>>> array
> [1, 2, 3, 4, 5]

You usually don't do things like that in python as far as I know.

You just work on a generated modified list.

foo = range(1,6)
for i in [x*2 for x in foo]:
   do_whatever_you_want_with(i)


--
~noufal
_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to