DEV Community

Super Kai (Kazuya Ito)
Super Kai (Kazuya Ito)

Posted on • Edited on

[] & () for variables in Python variable assignment

Buy Me a Coffee

  • My post explains [] and () for variables in for statement.
  • My post explains variable assignment.
  • My post explains iterable unpacking in variable assignment.
  • My post explains * for iterable unpacking in variable assignment.

You can use one or more [] and () for one or more variables in a variable assignment as shown below:

mylist = [[[5]]] v1 = mylist (v1) = mylist print(v1) # [[[5]]]  [v1] = mylist (v1,) = mylist print(v1) # [[5]]  [[v1]] = mylist ((v1,),) = mylist print(v1) # [5]  [[[v1]]] = mylist (((v1,),),) = mylist print(v1) # 5 
Enter fullscreen mode Exit fullscreen mode
mytuple = (((5,),),) v1 = mytuple (v1) = mytuple print(v1) # (((5,),),)  [v1] = mytuple (v1,) = mytuple print(v1) # ((5,),)  [[v1]] = mytuple ((v1,),) = mytuple print(v1) # (5,)  [[[v1]]] = mytuple (((v1,),),) = mytuple print(v1) # 5 
Enter fullscreen mode Exit fullscreen mode
mylist = [0, 1, [2, 3, [4, 5]]] v1 = mylist print(v1) # [0, 1, [2, 3, [4, 5]]]  [v1, v2, v3] = mylist (v1, v2, v3) = mylist print(v1, v2, v3) # 0 1 [2, 3, [4, 5]]  [v1, v2, [v3, v4, v5]] = mylist (v1, v2, (v3, v4, v5)) = mylist print(v1, v2, v3, v4, v5) # 0 1 2 3 [4, 5]  [v1, v2, [v3, v4, [v5, v6]]] = mylist (v1, v2, (v3, v4, (v5, v6))) = mylist print(v1, v2, v3, v4, v5, v6) # 0 1 2 3 4 5 
Enter fullscreen mode Exit fullscreen mode
mytuple = (0, 1, (2, 3, (4, 5))) v1 = mytuple print(v1) # (0, 1, (2, 3, (4, 5)))  [v1, v2, v3] = mytuple (v1, v2, v3) = mytuple print(v1, v2, v3) # 0 1 (2, 3, (4, 5))  [v1, v2, [v3, v4, v5]] = mytuple (v1, v2, (v3, v4, v5)) = mytuple print(v1, v2, v3, v4, v5) # 0 1 2 3 (4, 5)  [v1, v2, [v3, v4, [v5, v6]]] = mytuple (v1, v2, (v3, v4, (v5, v6))) = mytuple print(v1, v2, v3, v4, v5, v6) # 0 1 2 3 4 5 
Enter fullscreen mode Exit fullscreen mode

Only one *variable can be used in each [] and () as shown below:

mylist = [0, 1, [2, 3, [4, 5]]] v1 = mylist *v1, = mylist *(v1), = mylist print(v1) # [0, 1, [2, 3, [4, 5]]]  [v1, *v2, v3] = mylist (v1, *v2, v3) = mylist print(v1, v2, v3) # 0 [1] [2, 3, [4, 5]]  [v1, *v2, [*v3, v4, v5]] = mylist (v1, *v2, (*v3, v4, v5)) = mylist print(v1, v2, v3, v4, v5) # 0 [1] [2] 3 [4, 5]  [v1, *v2, [*v3, v4, [v5, *v6]]] = mylist (v1, *v2, (*v3, v4, (v5, *v6))) = mylist print(v1, v2, v3, v4, v5, v6) # 0 [1] [2] 3 4 [5] 
Enter fullscreen mode Exit fullscreen mode
mytuple = (0, 1, (2, 3, (4, 5))) v1 = mytuple print(v1) # (0, 1, (2, 3, (4, 5)))  *v1, = mytuple *(v1), = mytuple print(v1) # (0, 1, (2, 3, (4, 5)))  [v1, *v2, v3] = mytuple (v1, *v2, v3) = mytuple print(v1, v2, v3) # 0 [1] (2, 3, (4, 5))  [v1, *v2, [*v3, v4, v5]] = mytuple (v1, *v2, (*v3, v4, v5)) = mytuple print(v1, v2, v3, v4, v5) # 0 [1] [2] 3 (4, 5)  [v1, *v2, [*v3, v4, [v5, *v6]]] = mytuple (v1, *v2, (*v3, v4, (v5, *v6))) = mytuple print(v1, v2, v3, v4, v5, v6) # 0 [1] [2] 3 4 [5] 
Enter fullscreen mode Exit fullscreen mode

One or more [] and () cannot be used for one or more function parameters as shown below:

def func([p]): pass # SyntaxError: invalid syntax  def func((p)): pass def func((p,)): pass # SyntaxError: Function parameters cannot be parenthesized 
Enter fullscreen mode Exit fullscreen mode

Top comments (0)