Python declare multiple variables

Python allow you to declare and initialize more than one variable at the time. The syntax is simple but it has some details which are important. You can declare variables without type in Python and you can use them before declaration. Every variable in Python is an object. All the examples are based on python version 3.6:

You can find video tutorial for the article here: The right way to declare multiple variables in Python

Simple declaration of multiple variables in Python

Declare multiple variables and assign different values

Below you can find several different ways of declaring variables in Python. They cover several different situations like:

  • declare and initialize variables with None
  • declare and initialize variables with int values
  • declare and initialize variables with mixed values
x, y, z = None, None, None
x, y, z = 1, 2, 3
x, y, z = 1, '2', ['I''m a list']
x, y = True, True

every variable corespondent to a value from the right side. Variables and values should be equal as numbers otherwise error will be raised. You can check section errors.

Declare multiple variables and assign one value

There's one more way of declaring multiple variables in Python and assign values. This way is when you have one and the same value for the variables:

x = y = z
x = y = z = 1
x = y = z = ['x']
x, y, z = True

Now lets check what will happen if we change one of the variables - are the rest changed too?

x, y, z = True, True, True
print(x, y, z)
y = 5
print(x, y, z)

result:

True True True
True 5 True

And now lets check the associated object with this variables:

x, y, z = True, True, True
print(id(x), id(y), id(z))
y = 5
print(id(x), id(y), id(z))

result in:

504119712 504119712 504119712
504119712 504374096 504119712

Using tuples, list and ranges for declaration of multiple variables

Python gives freedom and responsibility for the programmer. With languages like Python is good to know what should be done and what shouldn't be done. Lets have look on the next examples. Do you think that these examples are working:

x, y, z = (None,)*3
x, y, z = (True,)*3
x, y, z = range(0,3)
x, y, z = [1, 2, 3]

result is the same as:

x, y, z = None, None, None
x, y, z = True, True, True

Now lets see what will happen if only of the variables is changed:

x, y, z = (True,)*3
print(x, y, z)
y = False
print(x, y, z)

result:

True True True
True False True

Lets try the same but this time using a list:

x, y, z = ([],)*3
print(x, y, z)
y = [5]
print(x, y, z)

the result is:

[] [] []
[] [5] []

which is fine and expected.

Now lets check what objects are represented by this variables:

x, y, z = ([],)*3
print(id(x), id(y), id(z))
y = [5]
print(id(x), id(y), id(z))

result:

31227400 31227400 31227400
31227400 31094152 31227400

Which is not expected and can cause problems if you are not sure how to variables and object works

As a final note have in mind that both next examples are not equal:

x = {} 
y = {}
print(id(x), id(y))

result:

26920840 26920968

x = y = {}
print(id(x), id(y))

result:

26921352 26921352

Another example demonstrating the problem:

x = y = z = ['x']
print(x, y, z)
y.append(5)
print(x, y, z)
x.append('s')
print(x, y, z)

result:

['x'] ['x'] ['x']
['x', 5] ['x', 5] ['x', 5]
['x', 5, 's'] ['x', 5, 's'] ['x', 5, 's']

as you can see modifying one of the 3 changes also the rest unlike the next case:

x, y, z = ['x'], ['x'], ['x']
print(x, y, z)
y.append(5)
print(x, y, z)
x.append('s')
print(x, y, z)

result:

['x'] ['x'] ['x']
['x'] ['x', 5] ['x']
['x', 's'] ['x', 5] ['x']

Related errors to multiple declaration

Below are listed typical errors for multiple variable initialization in Python:

ValueError: not enough values to unpack (expected X, got Y)

There are three errors which are related to the multiple variable declaration. This error is result of this code:

x, y, z = []

result:

ValueError: not enough values to unpack (expected 3, got 0)

we are initializing the variables with empty list which not OK. To make it working we can do:

x, y, z = [1, 2, 3]

or

x, y, z = range(0,3)

result:

0, 1, 2

TypeError: 'int' object is not iterable

The second error is result of less values than variables:

x, y = 2

result:

TypeError: 'int' object is not iterable

the solution is the same as for the previous ones and be sure that number of values and variables is the same.

ValueError: too many values to unpack (expected X)

The second error is result of less values than variables:

x, y = True, True, True

which result in:

ValueError: too many values to unpack (expected 2)

the solution is the same as for the previous ones and be sure that number of values and variables is the same.