Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Initialize variables in one line? [duplicate]

Tags:

python

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

X = []
Y = []
like image 616
william007 Avatar asked Oct 27 '25 04:10

william007


2 Answers

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 = []
like image 164
vaultah Avatar answered Oct 29 '25 17:10

vaultah


You can use tuple unpacking (or multiple assignment):

X, Y = [], []
like image 45
falsetru Avatar answered Oct 29 '25 19:10

falsetru



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!