Spring Deals - hours only!Up to 70% off on all courses and bundles.-Close
Counting with dictionaries
Grouping with dictionaries
Linking with dictionaries
15. Linking with dictionaries


Great! It's time to see the code for linking dictionaries in Python. Suppose we have an application where the user is asked to provide their weight, height, and age. We want to have some default values in case the user provides nothing. Take a look:

default_profile_dict = {'weight': 80.0, 'height': 1.82, 'age': 28.0}
user_input_dict = {}

for key in default_profile_dict.keys():
  user_input = input('Enter ' + key + ' or press ENTER for default: ')
  if user_input != '':
    user_input_dict[key] = float(user_input)

user_profile = default_profile_dict.copy()

In the example above, default_profile_dict is a dictionary with three default values. Next, we create a loop, iterate over each key in the default dictionary, and ask the user to provide a custom value. If the user does provide a value, we store it in a new dictionary, user_input_dict.

The three last lines show the actual process of linking: we start by copying the default values into the new user_profile dictionary with copy(). Then we use update() to update user_profile with values from user_input_dict for the corresponding keys. For instance, if the user only provides the value for weight, just the user's weight will be updated; the other values will remain as default values.


Complete the function named get_regional_settings(computer_settings, user_settings), whose stub is provided in the template code.

The function should return a regional settings dictionary for a given user. It should contain three elements: language, currency, and timezone. The default values are given in the template code. The logic is as follows:

  1. If there is a value for the given key in user_settings, use that setting.
  2. If there is no value in user_settings, use the value in computer_settings.
  3. If there is no value in computer_settings, use the default value.

You can test your function with the samples given in the template code.

Stuck? Here's a hint!

First, update the dictionary with the computer settings. Then update the dictionary with the user settings.