From the course: Programming Foundations: Data Structures (2023)

Unlock the full course today

Join today to access over 24,800 courses taught by industry experts.

Mutate a dictionary in Python

Mutate a dictionary in Python

- [Instructor] We can use dictionaries to store user preferences. Unlike our last example, the values are of different types. One key has a numerical value, another has a Boolean value, and the rest are strings. This is a flexibility that can be achieved in Python, but it doesn't work in all languages. In our last example, we worked with state capitals, but these state capitals are unlikely to change. User preferences, however, could change at any moment. Let's mutate our dictionary to change some of these preferences. To change the value of a key, we access the dictionary and use the key as index. Let's change the language to Spanish. We'll write out the dictionary name, user preferences, and then the key, language. We'll set its value to Spanish with the assignment operator. We could do this for any value in the dictionary as long as we know the key. Let's change the volume to 50. This overwrites the previous value so…

Contents