0

I want to append a list into another list using nested for loops but the code is not working as expected

def apend():
    ls = []
    numbers = [0,0]
    
    for num1 in range(3):
        for num2 in range(2):
            numbers[0] = num1
            numbers[1] = num2
            ls.append(numbers)
    print(ls)

apend()

I expect the output to be: [[0,0],[0,1],[0,2],1,0],[1,1],[1,2]]

but i get this output: [[1, 2], [1, 2], [1, 2], [1, 2], [1, 2], [1, 2]]

4
  • I think it's related to docs.python-guide.org/writing/gotchas/#late-binding-closures . Maybe try to instantiate the numbers in your inner for loop. Commented Jun 27, 2020 at 15:06
  • 3
    You are appending a reference to the same list in each iteration. Just append [num1, num2] instead. Commented Jun 27, 2020 at 15:06
  • can someone explain me please why this is not working Commented Jun 27, 2020 at 15:15
  • ls keeps it as reference to the same numbers but you should create new number before you put it in ls. OR do ls.append([num1, num2]) BTW: run your code on pythontutor.com and you will see references as arrows on visualization - and all arrows with refere to the same list number Commented Jun 27, 2020 at 15:20

4 Answers 4

2

If you run your code on http://pythontutor.com/ then you see

enter image description here

All elements in list ls keep reference to the same list numbers and this makes problem.

You have to create new list numbers = [0,0] inside for-loop

for num1 in range(3):
    for num2 in range(2):
        numbers = [0,0]
        numbers[0] = num1
        numbers[1] = num2
        ls.append(numbers)

Or simply do it without numbers

for num1 in range(3):
    for num2 in range(2):
        ls.append([num1, num2])
Sign up to request clarification or add additional context in comments.

Comments

1

Just change the numbers to numbers[:] and you will get you output as expected.

ls.append(numbers) means you are appending the reference of the list . so when the list changes , all instace changes . when you do ls.append(numbers[:]) this appends a copy of numbers.

def apend():
    ls = []
    numbers = [0,0]
    
    for num1 in range(3):
        for num2 in range(2):
            numbers[0] = num1
            numbers[1] = num2
            ls.append(numbers[:])
    print(ls)

apend()

Comments

0

You are appending the list named numbers in the loop. Just append a new list object as below.

def apend():
    ls = []
    numbers = [0,0]
    
    for num1 in range(3):
        for num2 in range(2):
            numbers[0] = num1
            numbers[1] = num2
            ls.append(list(numbers)) # append a new list object 
    print(ls)

By the way, I'd like to use list comprehension for this function.

def append():
    ls = [[x, y] for x in range(3) for y in range(2)]
    print(ls)

Comments

0

You can do it it in this way :

def apend():
    ls=[]
    numbers=[0,0]
    for num1 in range(3):
        for num2 in range(2):
            numbers[0]=num1
            numbers[1]=num2
            ls.append(num.copy())
    print(ls)
apend()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.