How do I change elements in a list?
Bruno Desthuilliers
bdesth.quelquechose at free.quelquepart.fr
Tue Nov 6 16:32:45 EST 2007
Just Another Victim of the Ambient Morality a écrit :
> How do you change certain elements in a list? I'm looking to do the
> Python equivalent of this Ruby code:
>
>
> -> first = [1, 2]
> => [1, 2]
> -> second = first
> => [1, 2]
> -> first.map! {|i| i + 1}
> => [2, 3]
> -> first
> => [2, 3]
> -> second
> => [2, 3]
>
>
> I need to change a list, in place, so other variables referencing that
> list also see the change.
first = [1,2]
second = first
first[:] = [i+1 for i in first]
HTH
More information about the Python-list
mailing list