0

I tried to debug the code with some print statements. So, I am presenting the whole code here.

l = ['one', 'two', 'three',  'four', 'five']

l1 = list()
count = 0
l2 = list()

for r in range(len(l)):

    print(r, 'this is r')
    print()
    if count < 2:
   
        l1.append(l[r])
        print(l1)
        print(l2)  #when the loop runs for the third time l2 is automatically updating to l1, which 
                   #is now ['one','two,'three'].Why?
        
        count += 1
        
    if count == 2:
    
        l2.append(l1)
        print(l2, 'this is l2')
        print()
        count = 0
    
print(l2)         
    

I think the output of l2 should be : [['one', 'two'], ['one', 'two', 'three', 'four']]

But the output I am getting is : [['one', 'two', 'three', 'four', 'five'], ['one', 'two', 'three', 'four', 'five']]

0 Answers0