[Tutor] insering into lists through slices
Pujo Aji
ajikoe at gmail.com
Fri Jun 3 19:25:05 CEST 2005
when you operate slice be carefull of the position of pointer which
slice the list:
L = [ 0 , 1 , 2 , 3]
^ ^
pos 0 1
That's why :
L = [1,2,3]
L[0:1] = [7]
print L # will replace element 1
L = [1,2,3]
L[1:1] = [7]
print L # will insert 7 between element 1 and 2
pujo
On 6/3/05, venkata subramanian <venkatasubramanian at gmail.com> wrote:
> Hi,
> I am a newbie to Python (that's very guessable).
> I simply fail to understand the semantics of the following piece of code.
> #assuming ls=[1,2,3,4]
> >>>ls[1:1]=[5,6]
> #then ls becomes
> >>> ls
> [1, 5, 6, 2, 3, 4]
>
> i would be happy to know how it works.
>
> Basically, ls[1:1] returns an empty list and assigning [5,6] to it,
> actually inserts the elements... but how?
> _______________________________________________
> Tutor maillist - Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
More information about the Tutor
mailing list