Dictionaries
1. Dictionaries
We've been using lists to store multiple values, but what if there is a relationship between values?2. Products list
Recall our products list contains product prices. What if we want to associate these prices with the IDs of the respective products? This structure doesn't make it clear that the price of the product with the first ID is 10 dollars. This is where we can use another data type!3. Dictionary
In Python, a dictionary is a data structure consisting of key-value pairs. It works the same as a normal dictionary - we can look up a word, which would be a key, and get its associated definition or value.4. Why use dictionaries?
Dictionaries are useful because the keys and values are associated. For example, to store information about purchases from an e-commerce website, we could use a dictionary with keys such as user ID, order number, date, value, and payment method, or to store IP addresses and locations for traffic to a social media site.5. Creating a dictionary
To create a dictionary we name our variable and use an equals sign, as usual.6. Creating a dictionary
Instead of the square brackets we used to make a list, we use curly brackets.7. Creating a dictionary
Inside, we provide our first key, as a string. In this case, the first product ID.8. Creating a dictionary
To associate a value with the key we follow this with a colon,9. Creating a dictionary
after which we enter the associated value, the price. This syntax makes a key-value pair.10. Creating a dictionary
To add more pairs, we add a comma,11. Creating a dictionary
then provide more keys and values using the same syntax,12. Creating a dictionary
and so on to complete the dictionary,13. Creating a dictionary
finishing with a closing curly bracket.14. Accessing a value based on the key
A dictionary consists of key-value pairs, and the order remains unchanged once set. This means we can access a value by subsetting by the respective key. Say we want to find the price of a specific product based on its ID. We can subset the dictionary, again using square brackets, and instead of providing an index number, we provide the key. The output shows the product's price!15. Accessing all values
If we want to get all values in the dictionary, there is a helpful dictionary method. We can use the dictionary's dot-values method, which returns only the values without their associated keys.16. Accessing all keys
Alternatively, we can use the dot-keys method to retrieve all keys in a dictionary.17. Viewing an entire dictionary
To view the entire dictionary, we can print it. However, there's also a method called dot-items available. This approach returns a list of all key-value pairs, where each key-value pair is shown as comma separated values inside parentheses. Values in parentheses are known as a tuple, and we'll learn about this data structure in the next video. For now, it's worth noting that dot-items is useful when we are looping or iterating over a variable, which is also something that we'll discuss later in the course.18. Adding a key-value pair
We can also update a dictionary to add a new key-value pair by using our typical assignment syntax with the variable name and an equals sign. However, as we are creating new information in a dictionary rather than making a new one, we use square brackets to subset, providing the new key name, in this case, another product ID.19. Updating a value
The same syntax can be used to update the value associated with an existing key. If product ID "HT91" is discounted then we can assign it a new price like so.20. Duplicate keys
Note that dictionaries do not accept duplicate keys. If we have two identical keys, like here with product ID AG32, then the most recent value will be the one assigned.21. Let's practice!
Let's build dictionaries in some exercises!Create Your Free Account
or
By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.