3

I am Python beginner with no prior programming experience. I apologize in advance if this question sounds too lame to answer to, but there is something I am confused with. It's related to adding a particular list (l2) to an empty list (l1). What I am trying to do is to increase the value of each of the "l2" elements in each iteration, and then add it to the l2 list.Here is the code:

l1 = []
l2 = [0,1,2,3]

for i in range(3):
    l2[0] = l2[0] + 1
    l2[1] = l2[1] + 1
    l2[2] = l2[2] + 1
    l2[3] = l2[3] + 1
    print l2
    l1.append(l2)

print l1

This is the print result that I get:

[1, 2, 3, 4]
[2, 3, 4, 5]
[3, 4, 5, 6]
[[3, 4, 5, 6], [3, 4, 5, 6], [3, 4, 5, 6]]

Why doesn't l2 list look like this:

[[1, 2, 3, 4], [2, 3, 4, 5], [3, 4, 5, 6]]

? Thank you for the reply.

3 Answers 3

5

It is because you are appending the same list object each time. You need to copy the list object before you append it, so that you are sure you append a different object.

l1.append(l2[:])
Sign up to request clarification or add additional context in comments.

Comments

4

That is because when you append l2 to l1, you are actually keeping the reference to the same list. Therefore, when you modify l2, the instance is the same as that which was appended to l1 and therefore that gets modified as well. You should do it as follows:

for i in range(3):
    l2[0] = l2[0] + 1
    l2[1] = l2[1] + 1
    l2[2] = l2[2] + 1
    l2[3] = l2[3] + 1
    print l2
    l1.append(l2[:])

>>> print l1
[[1, 2, 3, 4], [2, 3, 4, 5], [3, 4, 5, 6]]

Explanation

Here, the [:] is called list splicing. It follows the following format: [a:b] where a is the start index of a list and the b is the end index of the string (excluded). Without specifying any arguments, the [:] essentially creates a copy of the list and appends the copy.

Examples

>>> a = ['a', 'b', 'c', 'd', 'e']
>>> print a[2]
c

>>> print a[:]
['a', 'b', 'c', 'd', 'e']

>>> print a[2:4]
['c', 'd']

>>> print a[:3]
['a', 'b', 'c']

>>> print a[1:]
['b', 'c', 'd', 'e']

3 Comments

Thank you for an instant reply sshashank. But I am not sure I understand. Why does the "print l2" show the correct 'result' then? Is there a difference between the "l2" from "print l2" and "l2" from "l1.append(l2)"?
@user3137724, Because you are doing print l2 straight away. Whereas, you keep appending the values while also altering the previous values and the print it at the end.
@user3137724, You are in a sense appending the same object you are then later changing. What you want to do is create a copy of the object so far and then append that so that when you make future changes to the original l2 the copy that exists in your l1 does not get affected. It does take a while to wrap one's head around. Hope that was clear. Good Luck
1

Because you're adding the list l2 itself, and not a copy of it. That's why, at the end of the loop when you print it, you get a list with the last l2 values.

You can use this trick below to create a copy of l2 and then add it to l1:

l1.append(l2[:])

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.