[Tutor] iterate list items as lvalue
ricaraoz at gmail.com
Mon Aug 20 20:14:24 CEST 2007
Noufal Ibrahim wrote:
> 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
>> [2, 4, 6, 8, 10]
>> So I typed this:
>>>>> for item in array: item *= 2
>> [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]:
What about :
array = [1,2,3,4,5]
array = [i * 2 for i in array]
More information about the Tutor