

Output: ValueError: too many values to unpack (expected 2) Output: ValueError: not enough values to unpack (expected 6, got 3)Īs you can see, the unpacking failed because there are not enough values in the tuple to be assigned to 6 variables.Īlso, if the number of new variables is less than the number of elements in the tuple, unpacking fails.įor example, let’s try to pick two values from a tuple of five: coords = 1, 2, 3, 4, 5 Let’s see what happens when the number of variables exceeds the number of elements in the tuple: coords = 1, 2, 3 Make sure you always have the same number of elements on each side of the assignment operator when unpacking a tuple. The Number of Variables Must Equal to the Number of Elements This assigns the value 1 to x, 2 to y, and 3 to z. To make this work, the number of variables must match the number of items in the tuple.įor example, let’s pick all the 3D coordinate values from a tuple into separate variables x, y, and z: coords = 1, 2, 3 To do this, declare a comma-separated group of variable names and assign it to the tuple. If you need to pick all the values from a tuple into variables, use tuple unpacking. Here is the syntax for accessing elements from a tuple: tuple_valuesįor example, let’s get the first coordinate of the tuple that represents a 3D point: coords = 1, 2, 3
PYTHON TUPLE UNPACKING FREE
To learn more about tuples in Python, feel free to check this article. It is worthwhile to notice right from the get-go that a tuple does not necessarily need parenthesis.įor example, you can create the same tuple as above with: nums = 1, 2, 3 This means you cannot change the tuple in any way after it has been created.įor example, here is a tuple of three integers: nums = (1, 2, 3) The main difference between a tuple and a list in Python is that a tuple is immutable, whereas a list is mutable. Similar to a list, a tuple can store elements of different kinds. In Python, a tuple is one of the basic built-in types. Declare Multiple Values on the Same Line.Unpack Any Number of Elements with * Operator.The Number of Variables Must Equal to the Number of Elements.If there are no extra elements, an empty list is assigned. Note that even a single element assigned to a variable marked with an asterisk is assigned as a list. # *a, b, *c = t # Synta圎rror: two starred expressions in assignment If there are multiple variables marked with an asterisk, a Synta圎rror error will result because it is not possible to determine how many elements are assigned to each variable.

The same can also be written as follows a, b = t, t t = ( 0, 1, 2, 3, 4)įor example, if you want to assign only the first two elements of a tuple or list to a variable, you might use the above underscore for the parts that are not needed. The elements are assigned from the beginning and the end to the variables without asterisks, and the remaining elements are assigned as a list to the variables with asterisks.

This syntax has been implemented since Python 3 and is not available in Python 2. If the number of variables is less than the number of elements, an asterisk in the variable name will cause the elements to be assigned together as a list. There is no special grammatical meaning they are simply assigned to a variable named _. In Python, not only unpacked, values that are not needed are conventionally assigned to the underscore (underscore) _. # 0 # 1 # 2 # 3 # 4 Unpacked with _underscore_. If you want to unpack the contents as well, enclose the variable in one of the following Nested tuples and lists can also be unpacked. If the number of variables is less than the number of elements, the remaining elements can be assigned as a list by appending an asterisk to the variable name (see below). # a, b = t # ValueError: too many values to unpack (expected 2) # a, b, c, d = t # ValueError: not enough values to unpack (expected 4, got 3) If the number of variables does not match the number of elements, an error occurs.
