0
a = 2
b = 3
c = 4
x = y = z = [0 for i in xrange(a*b*c)]

Is there a way in which x,y,z can be initialized in one line (because I don't want to multiply a, b and c for each list initialization), as separate lists of 0s. In the above if x is updated, then y and z are also get updated simultaneously with the same changes.

Abhijay Ghildyal
  • 4,044
  • 6
  • 33
  • 54
  • 3
    Does this answer your question? [Python - Initializing Multiple Lists/Line](https://stackoverflow.com/questions/2402646/python-initializing-multiple-lists-line) – bers Dec 05 '19 at 10:29

2 Answers2

3

Just use another comprehension and unpack it:

x, y, z = [[0 for i in xrange(a*b*c)] for _ in xrange(3)]

Note that [0 for i in xrange(a*b*c)] is equivalent to the simpler [0] * a*b*c.

TigerhawkT3
  • 48,464
  • 6
  • 60
  • 97
  • how about using a generator so the list is not actually builded? `a, b, c = ([0 for i in xrange(a*b*c)] for _ in xrange(3))` – Netwave Aug 04 '16 at 10:19
  • @DanielSanchez - We could do that, certainly. However, seeing any performance benefit from it would require creating many more lists than should be unpacked into multiple independent variables in a single line. It only has to store the references anyway, which are small. – TigerhawkT3 Aug 04 '16 at 10:22
  • please can you explain a bit further, as I see it you are building the a list wich contains another lists, but you will never use that list. By using a generator you just get rid of that list. Am I wrong or missing something? thanks!! – Netwave Aug 04 '16 at 10:25
  • @DanielSanchez - It would get rid of a list, yes, but the list is tiny, and it should never be anything but tiny. – TigerhawkT3 Aug 04 '16 at 10:27
  • you meaned that, ok, either way I think it should not be used, just think it is in a loop of 1000 iterations, you will be creating 1000 lists, must take care of that. Thanks for the explanation, good answer. – Netwave Aug 04 '16 at 10:29
  • @DanielSanchez - It's not a loop with 1000 iterations for 1000 lists, it's a loop of 3 iterations for 3 lists. Remember that this is a single line - if there were more than half a dozen lists, it would start looking weird and taking up too much space. Also remember that this is being unpacked - if there were more than a dozen lists, it would make more sense to just retain the list holding them in the first place rather than unpacking it. – TigerhawkT3 Aug 04 '16 at 10:36
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/120104/discussion-between-daniel-sanchez-and-tigerhawkt3). – Netwave Aug 04 '16 at 10:40
1

Looking at your stated intention rather than at the 'one line' requirement:

a = 2
b = 3
c = 4
x = [0 for i in xrange(a*b*c)]
y = x [:]
z = x [:]

Not sure the optimizer is clever enough to avoid repeated multiplication at:

x, y, z = [[0 for i in xrange(a*b*c)] for _ in xrange(3)]

Suppose a, b, and c were properties, so reading them would have side effects. How could the optimizer know this in a dynamically typed language?

Jacques de Hooge
  • 6,750
  • 2
  • 28
  • 45