Dictionaries and Unpacking Arguments in Python

5/5 - (2 votes)

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}
# 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

Python Double Asterisk

Where to Go From Here?

Enough theory. Let’s get some practice!

Coders get paid six figures and more because they can solve problems more effectively using machine intelligence and automation.

To become more successful in coding, solve more real problems for real people. 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?

You build high-value coding skills by working on practical coding projects!

Do you want to stop learning with toy projects and focus on practical code projects that earn you money and solve real problems for people?

πŸš€ If your answer is YES!, consider becoming 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.

If you just want to learn about the freelancing opportunity, feel free to watch my free webinar β€œHow to Build Your High-Income Skill Python” and learn how I grew my coding business online and how you can, tooβ€”from the comfort of your own home.

Join the free webinar now!