[Tutor] Append to list

Rick Jaramillo rck.learn at gmail.com
Wed May 9 13:56:45 EDT 2018


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]].  

Best Regards,
Rick


More information about the Tutor mailing list