Tutorial - Tuple Unpacking in Python

in #utopian-io6 years ago (edited)

What Will I Learn?

A Python feature called tuple unpacking.

Requirements

  • Python 3+
  • A text editor you're comfortable with(example: Sublime Text, VSCode etc.)

Difficulty

  • Intermediate

Let's Start

Tuple unpacking is one of the most awesome features of Python and recently other languages like JavaScript are also trying to emulate this feature. Basically when we want to assign a value to a variable we do it like this:

x = 1

But what if we want to assign more than one variables at once. In that case, we can take advantage of the tuple unpacking feature available in python. How will we do that? Let's see:

x, y = 1, 2

This weird looking statement assigns 1 to x and 2 to y. If we look closely we can see that both side of the assignment operator = contains a tuple. Basically Python interpreter matches the index of the tuples and assigns the values accordingly.

In this case the first tuple is (x, y) and the second tuple is (1, 2). Index of x corresponds to the index of 1 so 1 gets assigned to x and index of y corresponds to the index of 2 so 2 gets assigned to y.

What if there are more values than variables?

What will happen when something like this occurs?

x, y = 1, 2, 3

If you guessed it raises an error you were absolutely right. The above code raises a ValueError. There are too many values in the second tuple. So, the python interpreter doesn't know how to handle that. We can solve it in two ways. we can tell python to store the first two values in x as a list:

*x, y = 1, 2, 3

or we can tell python to store the last two values in y as a list.

x, *y = 1, 2, 3

In this case, the python interpreter can take care of the tuples for us. Now, even if we provided the assignment with a tuple of more than 2 values it will never raise an error. Note the * in front of x and y in the latest operations. This operator is used with the variable name that you want to store as a list in your program.

Let's look at some examples now. Tuple Unpacking will become much more clearer after looking at these examples.

x, y = 1, 2                       # x = 1; y = 2;
*x, y = 1, 2, 3                # x = [1, 2]; y = 3;
x, *y = 1, 2, 3                # x = 1; y = [2, 3];
x, y, z = 1, 2, 3              # x = 1; y = 2; z = 3;
*x, y, z = 1, 2, 3, 4       # x = [1, 2]; y = 3; z = 4;
x, *y, z = 1, 2, 3, 4       # x = 1; y = [2, 3]; z = 4;
x, y, *z = 1, 2, 3, 4       # x = 1; y = 2; z = [3, 4];

What if there are more variables than values?

If there are more variables than values we can solve the situation similarly. Just with a use of *.
Let's look at some examples:

x = 1, 2                       # x = (1, 2);
*x, y, z = 1, 2                # x = []; y = 1; z = 2;
x, *y, z = 1, 2                # x = 1; y = []; z = 2;
x, y, z = 1, 2              # Will raise a ValueError

Tuple unpacking works with any Iterator type objects like strings, lists etc.

Many functions return tuples or lists. We can take advantage of the tuple unpacking feature to declare multiple variables at once. One such function is divmod() which is available to us by default. This function takes two arguments and returns a tuple consisting their quotient and remainder. Let's look at an example:

quotient, remainder = divmod(13, 2)
print(quotient)                     # 6
print(remainder)                # 1

The above example concludes our lesson on Python tuple unpacking. If you have any problems understanding any aspect of the tutorial please do comment. I would be happy to help you.

Thanks



Posted on Utopian.io - Rewarding Open Source Contributors

Sort:  

Oh that is cool - I had no idea about the asterisk - very neat! :D

Thanks. Glad to help. 😀

Your contribution cannot be approved because it does not follow the Utopian Rules.

Violated Rule:

  • Design or video editing related tutorials, gameplay, simple on-screen instructions, ubiquitous functions (Save, Open, Print, etc.) or basic programming concepts (variables, operators, loops, etc.) will not be accepted.

My Opinion:

  • A tutorial must be informative and explanatory, but also "tutor". This tutorial lacks "tutor"ing, and it is nothing more than an explanation of a documentation.

You can contact us on Discord.

[utopian-moderator]

Coin Marketplace

STEEM 0.20
TRX 0.14
JST 0.029
BTC 67494.12
ETH 3222.64
USDT 1.00
SBD 2.65