Dictionaries and Unpacking Arguments in Python

Programming is about using lower-level functionality to create higher-level functionality. In general, any programming language is a collection of functions that in turn build upon functions provided by the operating system. You must master the art of building your own code with the help of existing functionality, instead of reinventing the wheel!

Keyword Arguments

Functions are generic code snippets that can be tailored to your needs via keyword arguments.

The code snippet shows a function that calculates the sum of three keyword arguments. The keyword arguments are initialized with a default value in case they are not defined by the function caller.

def f(val1=3, val2=4, val3=6):
    return val1 + val2 + val3

values = {"val1": 9, "val3": -1}
print(f(**values))
# 12

The code snippet introduces two concepts: dictionaries and unpacking keyword arguments.

  • Dictionaries are Python data structures, defined via the bracket notation {}, that store key-value pairs. Python dictionaries work like real-world dictionaries: the keys are the words and the values are the explanations. You access the explanation to a given word via the index table. Similarly, in a Python dictionary, you access the values using the method of indexing. The indices (or keys) can be strings, integers, or any other immutable data type.
  • An interesting twist in the code snippet is to deliver keyword arguments via a dictionary using the double asterisk **-operator. The **-operator unpacks the key-value pairs in the dictionary and matches those with the keyword arguments. As the second keyword argument val2 is not declared in the dictionary, it is initialized to its default value.

In the code puzzle, you deliver values for keyword arguments val1=9 and val3=-1 by unpacking the values in the dictionary. The keyword argument val2 remains unchanged and receives the default value 4. The sum 9+4-1=12 which is the output of the code snippet.

Interactive Code Shell Unpacking Keyword Arguments

The following code shell is faulty and produces an error. Can you fix the dictionary to get rid of the error?

Python Puzzle Unpacking Keyword Arguments

Python puzzles help you accelerate your learning and lead to longer retention of the learning material. You can solve a puzzle related to this topic here:

Click the following link and test your skills now!

Related Video Asterisk

Where to Go From Here?

Enough theory, let’s get some practice!

To become successful in coding, you need to get out there and solve real problems for real people. That’s how you can become a six-figure earner easily. And that’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?

Practice projects is how you sharpen your saw in coding!

Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people?

Then become a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.

Join my free webinar “How to Build Your High-Income Skill Python” and watch how I grew my coding business online and how you can, too—from the comfort of your own home.

Join the free webinar now!