4

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

X = []
Y = []
william007
  • 17,375
  • 25
  • 118
  • 194
  • 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

8

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
  • 44,105
  • 12
  • 114
  • 143
2

You can use tuple unpacking (or multiple assignment):

X, Y = [], []
falsetru
  • 357,413
  • 63
  • 732
  • 636