Search code examples
pythonpython-3.xdeclarationdeclare

Python - Declare two variable with the same values at the same time


a=[1,2,3]
b=[1,2,3]

Is there a way to do this on one line? (obviously not with ";")

a,b=[1,2,3] 

doesn't work because of

a,b,c=[1,2,3]

a=1 b=2 c=3


Solution

  • In [18]: a,b=[1,2,3],[1,2,3]
    
    In [19]: a
    Out[19]: [1, 2, 3]
    
    In [20]: b
    Out[20]: [1, 2, 3]
    

    you may also want to do this:

    In [22]: a=b=[1,2,3]
    
    In [23]: a
    Out[23]: [1, 2, 3]
    
    In [24]: b
    Out[24]: [1, 2, 3]
    

    but be careful that, a is b is True in this case, namely, a is just a reference of b