Dictionary Methods
Now that you have learned about dictionaries and when you should use a dictionary. You will now look at a few other dictionary methods. To start with, you should follow the exercises from here onward to learn how to access the values and other related operations of a dictionary in Python.
Exercise 30: Accessing a Dictionary Using Dictionary Methods
In this exercise, we will learn how to access a dictionary using dictionary methods. The goal of the exercise is to print the order values against the item while accessing the dictionary by using dictionary methods:
- Open a new Jupyter Notebook.
- Enter the following code in a new cell:
orders = {'apple':5, 'orange':3, 'banana':2} print(orders.values()) print(list(orders.values()))
You should get the following output:
dict_values([5, 3, 2]) [5, 3, 2]
The
values()
method in this code returns an iterable object. In order to use the values straight away, you can wrap them in a list...