Unpacking With the Asterisk Operators
You are now able to use
**kwargs to define Python functions that take a varying number of input arguments. Let’s go a little deeper to understand something more about the unpacking operators.
00:11 The single and double asterisk unpacking operators were introduced in Python 2. As of the 3.5 release, they have become even more powerful, thanks to PEP 448, which you should check out if you’re interested. In short, the unpacking operators are operators that unpack the values from iterable objects in Python.
In this case, the output is no longer the list itself, but rather the content of the list. Can you see the difference between this execution and the previous one? Instead of a list,
print() now has taken three separate arguments as the input.
Another thing you’ll notice is that you used the
* unpacking operator to call a function instead of in a function definition. In this case, the
print() function takes all the items of a list as though they were single arguments. You can also use this method to call your own functions, but if your function requires a specific number of arguments, then the iterable you unpack must have the same number of arguments. To test this behavior, consider this script. Here,
my_sum() explicitly states that
c are required arguments. If you run this script, you’ll get the sum of the three numbers in
The three elements in
my_list match up perfectly with the required arguments in
my_sum(). Now look at this script, where
my_list has four arguments instead of three. In this example, the
my_sum() function still expects just three arguments, but the
* operator gets four items from the list.
02:23 This means that you can use multiple unpacking operators to get values from several lists and pass them all to a single function. To test this behavior, consider this example. If you run this example, all three lists are unpacked.
There are other convenient uses of the unpacking operator. For example, say you need to split a list into three different parts. The output should show the first value, the last value, and all the values in between. With the unpacking operator, you can do this in just one line of code. In this example,
my_list contains six items. The first variable is assigned to
a, the last to
c, and all other values are packed into a new list
b. If you run the script, the
print() function will show you that your three variables have the values that you would expect.
You can even merge two different dictionaries by using the
** unpacking operator. Here, the iterables to merge are
my_second_dict. Executing this code outputs a merged dictionary.
Remember that the
* operator works on any iterable object. It can also be used to unpack a string. In Python, strings are iterable objects, so a
* will unpack it and place all individual values in a list called
a. So if we run this script, then we can see our full list.
04:14 The previous example seems great, but when you work with these operators it’s important to keep in mind the seventh rule of the Zen of Python by Tim Peters, which is that “Readability counts.” To see why, consider this example.
The comma after the
a does the trick. When you use the unpacking operator with variable assignment, Python requires that your resulting variable is either a list or a tuple. With the trailing comma, you have actually defined a tuple with just one named variable
a. While this is a neat trick, many Pythonistas would not consider this code to be very readable. As such, it’s best to use these kinds of constructions sparingly.
Become a Member to join the conversation.