2

Is it possible do the following initializations in one line? As I am quite curious whether a shorter code is possible.

X = []
Y = []
william007
  • 15,661
  • 20
  • 90
  • 161
  • Possible duplicate: http://stackoverflow.com/questions/2402646/python-initializing-multiple-lists-line – Unapedra Oct 09 '14 at 15:26
  • Also [Understanding multiple variable assignment on one line in Python](http://stackoverflow.com/q/24587972), ["variable, variable =" syntax in python?](http://stackoverflow.com/q/12923059) – Martijn Pieters Oct 09 '14 at 15:29

2 Answers2

6

You can initialize them using sequence unpacking (tuple unpacking in this case)

X, Y = [], []

because it's equivalent to

(X, Y) = ([], [])

You can also use a semicolon to join lines in your example:

X = []; Y = []
vaultah
  • 40,483
  • 12
  • 109
  • 137
2

You can use tuple unpacking (or multiple assignment):

X, Y = [], []
falsetru
  • 336,967
  • 57
  • 673
  • 597