Data Unpacking

Multiple assignment

Till now, we have been assigning values to variables as follows:

But the same thing can be done in a single statement as follows:

Here, we are performing multiple assignments on two variables at once. 23 will be assigned to variable a and 54 will be assigned to variable b. Below is another valid example:

Here, the sum of a and b is assigned to c and the difference is assigned to d. It is to be noted that the right hand side expressions are evaluated first and then assigned to the left hand side variables.

Copying lists and strings
We have seen that lists and strings support slicing.

It slices the list or string from the specified starting index to the specified ending index but not including the ending index.

But we can also use the slice operation to create a copy of a list or string:

Sequence unpacking

Data in sequence data types(list, string and tuple) can be unpacked or broken down into separate values in Python. 

Let’s take an example of a list.

Here, in the second line when we assign nums to a,b, the list nums is unpacked and the respective values are assigned to a and b. There are two elements in the list and therefore, we need two variables a and b. It is to be noted that the number of variables must be equal to the number of elements in the list. Otherwise, it will throw an error.

ValueError: too many values to unpack (expected 2)

Here, the number of variables(2) is less than the number of elements being unpacked(3). The two variables cannot hold three values. That’s why we get an error as above.

ValueError: not enough values to unpack (expected 4, got 3)

Here, the number of variables(4) is more than the numbers of elements being unpacked(3). The unpacking assignment was expecting four values but received only three values.

Sequence unpacking using * (unpacking operator)

Sequence data types(list,str and tuple) can be unpacked using the * operator. Let us see them one by one.

The * operator also helps us to unpack lists. It helps us to separate the values as follows:

Output:

1 2 3 4 5

Here, all the elements of the list are printed as separate numbers. This means that we can create a copy of a list as follows:

But this can also be done as follows in Python:

In this approach, we are assigning the list nums to a variable a which in turn is unpacked using the * operator and placing the values inside a pair of square brackets. The square brackets enclosing the unpacked values creates

 a new list. This ensures that the left hand side of the assignment is a list. In Python, a trailing comma after a value also denotes a list(or a tuple). So, we can modify the above code as follows:

Here, the trailing comma indicates that a is a list which contains the unpacked elements of nums list. We are assigning the value of nums to a single variable(a). But we can also perform multiple assignments that we learnt earlier. Let us see what happens when we perform multiple assignments while unpacking.

In this example, we are assigning the value nums to *a and b. Here, b is known as a mandatory variable and must hold a value. As we have placed *a before b, b will hold the last element of the nums list and a will hold all the remaining values as a list.

In this example, we are assigning the value nums to a and *b. Here, a is the mandatory variable and must hold a value. As we have placed a before *b, a will hold the first element of the nums list and *b will hold all the remaining values as a list.

Here, a and c are mandatory variables. So, they will hold the first and last elements respectively and *b will hold the remaining elements as a list.

It is to be noted that the left hand side can have only one unpack operation.

String unpacking

Strings can also be unpacked just like lists. The same rules apply here. The number of variables should be equal to the number of items to be unpacked.

Using the * operator

Output:

C a t

Using the * operator separates each character of a string as shown above.

We can create a list out of all characters of a string as follows:

This can be done in the following way as well.

Here, the trailing comma indicates that we are creating a list from the unpacked values.

Multiple assignment works the same way in string as it works in lists.

Here, b is the mandatory variable which holds the last character and a holds the remaining characters(as a list) as *a is placed before b.

Similarly, here a holds the first character and b holds the remaining characters as a list just like what we saw in the case of lists.

Here, a and b are mandatory variables. So, they hold the first and last characters respectively. The variable b holds the remaining characters as a list.

Tuple unpacking

Tuples can also be unpacked in a similar way. It is to be noted that tuples can be created with or without parentheses.

Unpacking works in a similar way that we saw in lists. If the number of variables are not equal to the number of values being unpacked, Python will throw an error.

Using the * operator

The * operator works the same way in unpacking tuples just like lists and strings.

Output:

1 2 3

The * operator separates each value of the tuple as shown above.

We can create a list out of tuple values as follows using the * operator:

Here, we are unpacking the tuple values and putting them inside square brackets to create a new list. This can also be done as follows:

Using a trailing comma gives us the same output:

Here, the trailing comma indicates that a is a list which contains the unpacked elements of the tuple.

Multiple assignments work the same way as in lists.

Here, v is a mandatory variable and therefore it holds the last element and u holds the remaining elements as a list.

Dictionary unpacking

Dictionaries can be unpacked using multiple assignments to retrieve the individual keys. The number of variables must be equal to the number of keys being unpacked. Otherwise, we will get an error just like previous cases.

Here, we can see that only the keys are assigned to a and b. If we want to access the values after unpacking, we can do as follows:

Dictionary unpacking using ** operator

Dictionaries can similarly be unpacked using the ** operator. For example, we can create a copy of a dictionary by using the ** operator as follows:

Output:

{‘firstname’: ‘Charles’, ‘lastname’: ‘Babbage’}

Here, we are unpacking the key-value pairs of the dictionary and enclosing them within curly braces. This way we are creating a new dictionary which we are assigning to the variable p.

We can merge two dictionaries using the unpacking operator:

Output:

{‘firstname’: ‘Charles’, ‘lastname’: ‘Babbage’, ‘country’: ‘United Kingdom’, ‘invention’: ‘Difference Engine’}