Summary: in this tutorial, you’ll learn how to unpack tuples in Python. Show
Reviewing Python tuplesPython defines a tuple using commas (
Python uses the parentheses to make the tuple clearer:
Python also uses the parentheses to create an empty tuple:
In addition, you can use the
To define a tuple with only one element, you still need to use a comma. The following example illustrates how to define a tuple with one element:
It’s equivalent to the following:
Note that the following is an integer, not a tuple:
Unpacking a tupleUnpacking a tuple means splitting the tuple’s elements into individual variables. For example:
The left side:
is a tuple of two variables The right side is also a tuple of two integers The expression assigns the tuple elements on the right side (1, 2) to each variable on the left side (x, y) based on the relative position of each element. In the above example, See another example:
The right side is a tuple of three integers
Output:
In the above example, the Using unpacking tuple to swap values of two variablesTraditionally, to swap the values of two variables, you would use a temporary variable like this:
Output:
In Python, you can use the unpacking tuple syntax to achieve the same result:
Output:
The following expression swaps the values of two variables, x and y.
In this expression, Python evaluates the right-hand side first and then assigns the variable from the left-hand side to the values from the right-hand side. ValueError: too many values to unpackThe following example unpacks the elements of a tuple into variables. However, it’ll result in an error:
Error:
This error is because the right-hand side returns three values while the left-hand side only has two variables. To fix this, you can add a
The Typically, you use the dummy variable to unpack when you don’t care and use its value afterward. Extended unpacking using the * operatorSometimes, you don’t want to unpack every single item in a tuple. For example, you may want to unpack the first and second elements. In this case, you can use the
Output:
In this example, Python assigns Notice that you can only use the The following example results in error:
Error:
Using the * operator on the right hand sidePython allows you to use the
The following example uses the
Output:
Summary
Did you find this tutorial helpful ? How do I unpack a tuple?Unpacking tuples means assigning individual elements of a tuple to multiple variables. Use the * operator to assign remaining elements of an unpacking assignment into a list and assign it to a variable.
How do I extract data from a tuple in Python?Python - Unpack Tuples. ❮ Previous Next ❯. Packing a tuple: fruits = ("apple", "banana", "cherry") ... . Unpacking a tuple: fruits = ("apple", "banana", "cherry") ... . Assign the rest of the values as a list called "red": ... . Add a list of values the "tropic" variable: ... . ❮ Previous Next ❯. How does tuple unpacking work in Python?When we are unpacking values into variables using tuple unpacking, the number of variables on the left side tuple must exactly match the number of values on the right side tuple . Otherwise, we'll get a ValueError .
Is it possible to unpack tuple items using for loop?We can do the tuple unpacking right inside the for loop itself because anything you can put on the left-hand side of the equal sign, you can put in between the for and the in in a for loop.
|