In python, we can view a dictionary’s keys and values using the dictionary functions keys()
and values()
respectively. In this tutorial, we’ll look at how to use these functions along with some examples.
Before we proceed, here’s a quick refresher on dictionaries in python – Dictionaries are a collection of items used for storing key to value mappings. They are mutable and hence we can update the dictionary by adding new key-value pairs, removing existing key-value pairs, or changing the value corresponding to a key. For more, check out our guide on dictionaries and other data structures in python.
View Keys and Values of a python dictionary
The dictionary functions keys()
and values()
can be used to view a dictionary’s keys and values. They return a view object that provides a dynamic view of the dictionary’s entries, meaning when the dictionary changes, the view reflects these changes.
The keys() function
The python dictionary keys()
function gives a view object that displays a list of all the keys in the dictionary. The following is the syntax:
sample_dict.keys()
Here, sample_dict is the dictionary whose keys you want to get.
Parameters: The keys()
function does not take any parameters.
Returns: It returns a view object of the dictionary’s keys. For more on dictionary view objects, refer to the python docs.
Example 1: Using the keys() function to display a dictionary’s keys
Introductory ⭐
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
Intermediate ⭐⭐⭐
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
Advanced ⭐⭐⭐⭐⭐
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 Find Data Science Programs 👨💻 111,889 already enrolled
Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.
# dictionary of a sample portfolio
shares1 = {'APPL': 100, 'GOOG': 50}
shares2 = {}
# print the keys of the shares1 and shares2
print("Keys of shares1:", shares1.keys())
print("Keys of shares2:", shares2.keys())
Output:
Keys of shares1: dict_keys(['APPL', 'GOOG'])
Keys of shares2: dict_keys([])
In the above example, the keys()
function is used to get the keys of dictionaries shares1
and shares2
. We print out the returned objects for both the dictionaries. We see that for the dict shares1
, a view object with its keys in a list is returned while for the empty dict shares2
, a view object with an empty list is returned.
Example 2: When a dictionary is updated
# dictionary of a sample portfolio
shares = {'APPL': 100, 'GOOG': 50}
keys = shares.keys()
# print the keys of the shares
print("Keys of shares:", keys)
# update the dictionary
shares.update({'TSLA': 80})
# print the keys of the shares
print("Keys of shares:", keys)
Output:
Keys of shares: dict_keys(['APPL', 'GOOG'])
Keys of shares: dict_keys(['APPL', 'GOOG', 'TSLA'])
In the above example, we update the dictionary shares
and print its keys before and after the update. We see that keys
gets automatically updated when a change is made to the dictionary.
The values() function
The python dictionary values()
function gives a view object of the dictionary’s values. It has a similar syntax as that of the keys function:
sample_dict.values()
Here, sample_dict is the dictionary whose values you want to get.
Parameters: The values()
function does not take any parameters.
Returns: It returns a view object of the dictionary’s values.
Example 1: Display a dictionary’s values using values()
# dictionary of a sample portfolio
shares1 = {'APPL': 100, 'GOOG': 50}
shares2 = {}
# print the values of the shares1 and shares2
print("Values of shares1:", shares1.values())
print("Values of shares2:", shares2.values())
Output:
Values of shares1: dict_values([100, 50])
Values of shares2: dict_values([])
In the above example, the values()
function is used to get the values of shares1
and shares2
dictionaries. We see that for the empty dictionary we get view object with an empty list.
Example 2: When the dictionary is updated
# dictionary of a sample portfolio
shares = {'APPL': 100, 'GOOG': 50}
values = shares.values()
# print the values of the shares
print("Values of shares:", values)
# update the dictionary
shares.update({'TSLA': 80})
# print the values of the shares
print("Values of shares:", values)
Output:
Values of shares: dict_values([100, 50])
Values of shares: dict_values([100, 50, 80])
In the above example, we print the values of the dictionary shares
before and after the update. We see that values
gets automatically updated when a change is made to the dictionary.
Dictionary view objects behavior:
The dictionary view object can be iterated over and supports membership tests.
Example 1: Iterate over a dictionary keys
# dictionary of a sample portfolio
shares = {'APPL': 100, 'GOOG': 50}
# dictionary keys view object
keys = shares.keys()
# iterate over keys
for k in keys:
print(k)
Output:
APPL
GOOG
In the above example, we iterate over keys
which is a dictionary view object returned by the keys()
function.
Example 2: Membership test
# dictionary of a sample portfolio
shares = {'APPL': 100, 'GOOG': 50}
# dictionary keys view object
keys = shares.keys()
# Membership test using in
print('APPL' in keys)
Output:
True
In the above example, we test for the membership of a key in the view object returned by the keys()
function using the membership operator in
.
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.