How do I change elements in a list?

Dan thermostat at gmail.com
Tue Nov 6 22:24:07 CET 2007


On Nov 6, 4:11 pm, "Just Another Victim of the Ambient Morality"
<ihates... at hotmail.com> wrote:
>     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.
>     Thank you...


>>> first = [1,2]
>>> second = first
>>> for i in range(len(first)):
	first[i] += 1

>>> first
[2, 3]
>>> second
[2, 3]

-Dan




More information about the Python-list mailing list