Back-To-School Deals - hours only!Up to 80% off on all courses and bundles.-Close
Recap
Counting with dictionaries
Grouping with dictionaries
Linking with dictionaries
13. Linking dictionaries
Summary

Instruction

Finally, the last dictionary usage that we want to present is what's called linking.

Suppose we have an application whose behavior depends on the application settings. Application settings is a dictionary with key-value pairs. The application has some global defaults, but each user can fine-tune the settings in the user settings. Finally, user settings can be changed again on the project level. The picture below shows the different settings:

linking

The value of "editor" parameter is "sublime", because that's its value in the project settings which has the highest precendence. The value of the parameter "timeout" is 500: it's not defined in the project settings, but it is defined in user settings.

Linking dictionaries in creating a new dictionary with the current parameter values based on existing dictionaries which are chained in a specific order. In this section we'll learn how to link dictionaries in Python.

The first function we'll need is very simple: the function copy(). It creates a copy of the dictionary. Take a look:

defaults = {'weight': 80.0, 'height': 1.82, 'age': 28.0}
user_profile = defaults.copy()

The user_profile is a copy of the dictionary defaults.

Exercise

You are given a dictionary with salaries for 2017. Create a copy of this salary and name it salaries_2018. In 2018 everyone got a 10% rise. Update the 2018 dictionary to reflect the rise – each value in salaries_2018 should be an integer.

Finally, print both dictionaries and observe that the values in the dictionary salaries_2017 were not modified. This confirms that salaries_2018 is a separate dictionary.