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) 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. 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. 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. 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. As a complement, the term packing can be used when we collect several values in a single variable using the iterable unpacking operator, *. This may be useful to you as you can then create the dictionary programmatically.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. You can construct your argument list as a dictionary and pass that dictionary as an argument. This can be useful you have a function with some known arguments you need to pass in. For dictionaries, however we use the double asterisk: ‘**’ to unpack. It is also possible to unpack dictionaries using a similar shortcut. *x ‘unpacks’ the elements of the x list and places those into the argument of the func_test function. The effect of the * operator is similar in the function example. For example a list may contain elements that you want to fit into a print statement: There are times when you you want to separate out the elements in an iterator. Unpacking IterablesĪn iterator in python is an object like a list or a tuple – in other words, things you can iterate over. This post looks at unpacking iterables, and how it can also be used to make functions functions take a variable number of arguments. Python provides a way to ‘unpack’ these iterables which can be a useful shortcut in some situations. Python iterables are lists, tuples, dictionaries and other similar objects – in other words things that can be iterated over.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |