-1

While working on a project I've come across the following two list forms:

1)

l1=[10]*2
p1,p2=l1

2)

l2=[]
for i in xrange(2):
   l2.append(10)
p1,p2=l2

Both appear to have the same output (as in l1=l2) but when I print some graphs they are not the same, so there must be something. Any help or suggestion is greatly appreciated. The above code is a minimal working example.

George
  • 451
  • 1
  • 6
  • 17
  • 2
    What do you mean by "when I print some graphs they are not the same"? What graphs are you printing? When you print those graphs for the code you posted, do you see a difference? – user2357112 Sep 07 '18 at 17:47
  • 1
    It depends if the list contains mutable or immutable elements, see the above link. – Cory Kramer Sep 07 '18 at 17:47
  • @CoryKramer Well the question has immutable list items, so there should be no difference. I don't think this is a duplicate, but it is missing the information needed to understand the problem – zvone Sep 07 '18 at 18:04
  • @user2357112 I posted the above as a minimum working example, I don't use this exact script. The lists are part of some legacy code I'm tweaking and that's where the problem arose. – George Sep 07 '18 at 18:07
  • @George the code you posted does not have the problem you are explaining. You should provide a [minimal complete and verifiable example](http://stackoverflow.com/help/mcve) – zvone Sep 07 '18 at 18:31

1 Answers1

0

The first one means that the object isn't re-evaluated every time, however numbers always refer to a new object so this doesn't have any effect. However, take this:

l = [[0] * 3] * 3

This will make a list full of 3 lists containing 3 zeros. Now try changing one element:

l[0][0] = 1

>>> l[0]
[1, 0, 0]
>>> l[1]
[1, 0, 0]

You see that it affected the other list too. This is because the list being multiplied referred to the same object. But your example with a proper for loop re-evaluates each object and could save debugging problems

You could also do the following why does re-evaluate the object.

l = [0 for _ in range(3)]
N Chauhan
  • 3,407
  • 2
  • 7
  • 21
  • 1
    "however numbers always refer to a new object" - not true. It is unspecified whether evaluating a numeric literal will produce a new object. In the standard implementation of Python, it happens to be the case that every evaluation of a `10` literal will produce the same int object. – user2357112 Sep 07 '18 at 17:52
  • But they still function independently of each other, do they not? Unless obviously your code is `a = b = 10` but this uses variables that refer to the same object. – N Chauhan Sep 07 '18 at 17:53
  • 1
    There is no behavioral difference between `a = b = 10` and `a = 10; b = 10`. Numbers only seem to behave differently from lists because all of Python's built-in numeric types are immutable. There's no way to modify a `10` through one reference and have the change be visible through another reference, because you can't modify a `10` at all. – user2357112 Sep 07 '18 at 17:56