Reputation: 13433
In Python 3.x, can arrays be initialized on the same line?
I'm trying to be a "one-liner guy," but not certain of the subtleties between these two.
Minimal, complete, verifiable example:
Are these statements equivalent?
A:
def main():
a = b = []
B:
def main():
a = []
b = []
Upvotes: 1
Views: 113
Reputation: 51037
These two are not equivalent; the first one creates two references to the same list:
>>> a = b = []
>>> a.append(1)
>>> b
[1]
The second creates two lists:
>>> a = []
>>> b = []
>>> a.append(1)
>>> b
[]
To create two lists on one line, you can do a parallel assignment:
>>> a, b = [], []
>>> a.append(1)
>>> b
[]
Upvotes: 4