5) d. Unpacking Argument

Note: This is an advanced topic. If you have absolutely no prior programming experience you can go ahead and skip this topic. If you skip it you’ll still be fine with your Python learning. This topic just introduces another way of doing something that you already know. Later after you’re done with the whole course and gained an all round knowledge of Python you can always come back an attempt this concept.



In the last section you have learned how use * to pack arguments into a tuple.

You can also use * to unpack collections.

Take a look at this example.

1
2
3
Output:
[1, 2, 3, 4]
1 2 3 4





If you add an asterisk before your data structure (during a function call or in an expression) then the data structure gets unpacked.




Say you’re printing a list. You want to separate all elements with a newline character. You can’t use the print’s sep parameter because you’re giving it a list.
Instead, you can unpack a list using an asterisk. Now you are passing lots of values to print. Now you can set the sep parameter as a newline character. You’ll rarely be using this though.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
Output:
['C:/Program Files (x86)/Python36-32/', 'C:/WINDOWS/system32', 'C:/WINDOWS', 'C:/WINDOWS/System32/WindowsPowerShell/v1.0/', 'C:/Program Files/PuTTY/', 'C:/ProgramData/chocolatey/bin', 'c:/php7/', 'C:/ffmpeg/bin/', 'C:/Program Files (x86)/nodejs/', 'C:/IEDriverServer/', 'C:/geckoserver/', 'C:/chromedriver/', 'C:/WINDOWS/System32/OpenSSH/', 'C:/Program Files (x86)/Intel/Intel(R) Management Engine Components/DAL', 'C:/Program Files/Intel/Intel(R) Management Engine Components/DAL', 'C:/Windows/System32', 'C:/Program Files/Git/cmd', 'C:/tools/ruby24/bin', 'C:/Users/user1/AppData/Local/Microsoft/WindowsApps', 'C:/tools/mysql/current/bin', 'C:/Users/user1/AppData/Roaming/npm', 'C:/tools/ruby24/bin/', 'C:/Program Files/Mozilla Firefox/', 'C:/Program Files (x86)/Graphviz2.38/bin/', '%USERPROFILE%/AppData/Local/Microsoft/WindowsApps']



C:/Program Files (x86)/Python36-32/
C:/WINDOWS/system32
C:/WINDOWS
C:/WINDOWS/System32/WindowsPowerShell/v1.0/
C:/Program Files/PuTTY/
C:/ProgramData/chocolatey/bin
c:/php7/
C:/ffmpeg/bin/
C:/Program Files (x86)/nodejs/
C:/IEDriverServer/
C:/geckoserver/
C:/chromedriver/
C:/WINDOWS/System32/OpenSSH/
C:/Program Files (x86)/Intel/Intel(R) Management Engine Components/DAL
C:/Program Files/Intel/Intel(R) Management Engine Components/DAL
C:/Windows/System32
C:/Program Files/Git/cmd
C:/tools/ruby24/bin
C:/Users/user1/AppData/Local/Microsoft/WindowsApps
C:/tools/mysql/current/bin
C:/Users/user1/AppData/Roaming/npm
C:/tools/ruby24/bin/
C:/Program Files/Mozilla Firefox/
C:/Program Files (x86)/Graphviz2.38/bin/
%USERPROFILE%/AppData/Local/Microsoft/WindowsApps




Unpacking is more commonly used while calling a function. Here is another example:

1
2
Output:
9




But what happens if you try to unpack a dictionary?

1
a b c d




If you attempt to unpack a dictionary you will only unpack its keys.

To unpack the whole dictionary you need to use **. However that is almost only useful when calling a function with a pre-made dictionary of keyword arguments.

1
2
3
Output:
I've received the values: 1 2 3 4
I'
ve received the values: 1 2 3 4




Using this property we can create a function that can take any combination of positional and keyword arguments.

1
2
3
def universal_function(*args, **kwargs):
    # Parse args and kwargs.
    pass

Please note that using the names args and kwargs is a widely accepted Python naming convention.