On 9 May 2018, at 23:57, Rick Jaramillo <rck.le...@gmail.com> wrote:

>
>Hello,
>I’m having trouble understanding the following behavior and would greatly 
>appreciate any insight. 
>l = [1,2,3,4]
>b=[]
>for i in range(l):
>    print l
>    b.append(l)
>    l.pop(0)
>print b
>OUTPUT 
>[1,2,3,4]
>[2,3,4]
>[3,4]
>[4]
>[[],[],[],[]]
>My confusions is the output for b. I don’t understand why it’s empty. I’m 
>expecting for b to equal [[1,2,3,4], [2,3,4], [3,4], [4]].  

You append l each time. The same l, not a copy of its current state. You also 
modify l each time. But there is only one list object. It is just being 
referenced from several places. So all of the references reflect your changes.


Alan g
_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor

Reply via email to