gangoreo.blogg.se

Tuple unpacking python 3
Tuple unpacking python 3








  1. Tuple unpacking python 3 how to#
  2. Tuple unpacking python 3 code#

So, it's best to avoid using sets in unpacking operations unless the order of assignment isn't important to our code. If we use sets in unpacking operations, then the final order of the assignments can be quite different from what we want and expect. '3' > # Unpacking lists > a, b, c = ģ > # Unpacking generators > gen = (i ** 2 for i in range( 3))Ĥ > # Unpacking dictionaries (keys, values, and items) > my_dict = The only requirement is that the iterable yields exactly one item per variable in the receiving tuple (or list).Ĭheck out the following examples of how iterable unpacking works in Python: > # Unpacking strings > a, b, c = '123' > a The tuple unpacking feature got so popular among Python developers that the syntax was extended to work with any iterable object. That's because Python needs to unambiguously know what value goes into what variable, so it can do the assignment accordingly. If we use a different number of variables and values in a tuple unpacking operation, then we'll get a ValueError. ValueError: not enough values to unpack (expected 3, got 2) On the other hand, if we use more variables than values, then we'll get a ValueError but this time the message says that there are not enough values to unpack: > a, b, c = 1, 2 Note: The only exception to this is when we use the * operator to pack several values in one variable as we'll see later on. ValueError: too many values to unpack (expected 2)

tuple unpacking python 3

This will raise a ValueError telling us that there are too many values to unpack: > a, b = 1, 2, 3 Otherwise, we'll get a ValueError.įor example, in the following code, we use two variables on the left and three values on the right. 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. Arguably, the last syntax is more commonly used when it comes to unpacking in Python. Since all these variations are valid Python syntax, we can use any of them, depending on the situation. This also works for tuple unpacking, so the following syntaxes are equivalent: > (a, b, c) = 1, 2, 3 > a, b, c = ( 1, 2, 3) To create a tuple object, we don't need to use a pair of parentheses () as delimiters. As you can see in the above example, a will be 1, b will be 2, and c will be 3. The values on the right are assigned to the variables on the left according to their relative position in each tuple.

tuple unpacking python 3

When we put tuples on both sides of an assignment operator, a tuple unpacking operation takes place. Check out the following example: > (a, b, c) = ( 1, 2, 3) This is commonly known as tuple unpacking in Python. The values on the right will be automatically assigned to the variables on the left according to their position in the tuple. In Python, we can put a tuple of variables on the left side of an assignment operator ( =) and a tuple of values on the right side. Let's take a closer look to unpacking in Python and see how this feature can improve our code.

Tuple unpacking python 3 code#

Unpacking operations have been quite popular among Python developers because they can make our code more readable, and elegant. However, since this feature has been generalized to all kind of iterable, a more accurate term would be iterable unpacking and that's what we'll call it in this tutorial. Each variable in the tuple can receive one value (or more, if we use the * operator) from an iterable on the right side of the assignment.įor historical reasons, Python developers used to call this tuple unpacking. Python allows a tuple (or list) of variables to appear on the left side of an assignment operation.

Tuple unpacking python 3 how to#

In this tutorial, we'll learn what iterable unpacking is and how we can take advantage of this Python feature to make our code more readable, maintainable, and pythonic.Īdditionally, we'll also cover some practical examples of how to use the iterable unpacking feature in the context of assignments operations, for loops, function definitions, and function calls. Nowadays, a more modern and accurate term would be iterable unpacking. However, since this Python feature has turned out to be quite useful and popular, it's been generalized to all kinds of iterables. Historically, Python developers have generically referred to this kind of operation as tuple unpacking.

tuple unpacking python 3

As a complement, the term packing can be used when we collect several values in a single variable using the iterable unpacking operator, *. Unpacking in Python refers to an operation that consists of assigning an iterable of values to a tuple (or list) of variables in a single assignment statement.










Tuple unpacking python 3