newbe question about lists

Fredrik Lundh effbot at telia.com
Sun Mar 5 05:01:29 EST 2000


Fredrik Sj”stedt wrote:
> I'm going through a study course online, and ringt now I
> am trying lists.
>
> I type the following:
> >>>
> l1=["a1","a2","a3"]
> >>> l2=["a4","a5","a6"]
> >>> l3=l1
> >>>
> l3[1:1]=l2
> >>> l3
> ['a1', 'a3', 'a4', 'a2']
> >>> l1
> ['a1', 'a3',
> 'a4', 'a2']
>
> QUESTION:
> Why is value of l1 changed... The only
> thing I can think of is B U G ...

yes, it's a bug in your program:

the "l1 = ..." line creates a new list object.

the "l3 = l1" line makes l3 point to the same object as l1.

the "l3[1:1] = l2" line modifies that object in place.

</F>





More information about the Python-list mailing list