Skip to content

Instantly share code, notes, and snippets.

@marcusnapoleon
Created September 26, 2021 19:21
Show Gist options
  • Save marcusnapoleon/c853be76d586356884b1565b117d0b7f to your computer and use it in GitHub Desktop.
Save marcusnapoleon/c853be76d586356884b1565b117d0b7f to your computer and use it in GitHub Desktop.
IBM Data Engineer Professional Certificate - Coursera
Dictionaries in Python
Estimated time needed: 25 minutes
Objectives
After completing this lab you will be able to:
Work with and perform operations on dictionaries in Python
Table of Contents
Dictionaries
What are Dictionaries?
Keys
Quiz on Dictionaries
Dictionaries
What are Dictionaries?
A dictionary consists of keys and values. It is helpful to compare a dictionary to a list. Instead of being indexed numerically like a list, dictionaries have keys. These keys are the keys that are used to access values within a dictionary.
Image
An example of a Dictionary Dict:
# Create the dictionary
Dict = {"key1": 1, "key2": "2", "key3": [3, 3, 3], "key4": (4, 4, 4), ('key5'): 5, (0, 1): 6}
Dict
{'key1': 1,
'key2': '2',
'key3': [3, 3, 3],
'key4': (4, 4, 4),
'key5': 5,
(0, 1): 6}
The keys can be strings:
# Access to the value by the key
Dict["key1"]
1
Keys can also be any immutable object such as a tuple:
# Access to the value by the key
Dict[(0, 1)]
6
Each key is separated from its value by a colon ":". Commas separate the items, and the whole dictionary is enclosed in curly braces. An empty dictionary without any items is written with just two curly braces, like this "{}".
# Create a sample dictionary
release_year_dict = {"Thriller": "1982", "Back in Black": "1980", \
"The Dark Side of the Moon": "1973", "The Bodyguard": "1992", \
"Bat Out of Hell": "1977", "Their Greatest Hits (1971-1975)": "1976", \
"Saturday Night Fever": "1977", "Rumours": "1977"}
release_year_dict
{'Thriller': '1982',
'Back in Black': '1980',
'The Dark Side of the Moon': '1973',
'The Bodyguard': '1992',
'Bat Out of Hell': '1977',
'Their Greatest Hits (1971-1975)': '1976',
'Saturday Night Fever': '1977',
'Rumours': '1977'}
In summary, like a list, a dictionary holds a sequence of elements. Each element is represented by a key and its corresponding value. Dictionaries are created with two curly braces containing keys and values separated by a colon. For every key, there can only be one single value, however, multiple keys can hold the same value. Keys can only be strings, numbers, or tuples, but values can be any data type.
It is helpful to visualize the dictionary as a table, as in the following image. The first column represents the keys, the second column represents the values.
Image
Keys
You can retrieve the values based on the names:
# Get value by keys
release_year_dict['Thriller']
'1982'
This corresponds to:
Image
Similarly for The Bodyguard
# Get value by key
release_year_dict['The Bodyguard']
'1992'
Image
Now let us retrieve the keys of the dictionary using the method keys():
# Get all the keys in dictionary
release_year_dict.keys()
dict_keys(['Thriller', 'Back in Black', 'The Dark Side of the Moon', 'The Bodyguard', 'Bat Out of Hell', 'Their Greatest Hits (1971-1975)', 'Saturday Night Fever', 'Rumours'])
You can retrieve the values using the method values():
# Get all the values in dictionary
release_year_dict.values()
dict_values(['1982', '1980', '1973', '1992', '1977', '1976', '1977', '1977'])
We can add an entry:
# Append value with key into dictionary
release_year_dict['Graduation'] = '2007'
release_year_dict
{'Thriller': '1982',
'Back in Black': '1980',
'The Dark Side of the Moon': '1973',
'The Bodyguard': '1992',
'Bat Out of Hell': '1977',
'Their Greatest Hits (1971-1975)': '1976',
'Saturday Night Fever': '1977',
'Rumours': '1977',
'Graduation': '2007'}
We can delete an entry:
# Delete entries by key
del(release_year_dict['Thriller'])
del(release_year_dict['Graduation'])
release_year_dict
{'Back in Black': '1980',
'The Dark Side of the Moon': '1973',
'The Bodyguard': '1992',
'Bat Out of Hell': '1977',
'Their Greatest Hits (1971-1975)': '1976',
'Saturday Night Fever': '1977',
'Rumours': '1977'}
We can verify if an element is in the dictionary:
# Verify the key is in the dictionary
'The Bodyguard' in release_year_dict
True
Did you know? IBM Watson Studio lets you build and deploy an AI solution, using the best of open source and IBM software and giving your team a single environment to work in. Learn more here.
Quiz on Dictionaries
You will need this dictionary for the next two questions:
# Question sample dictionary
soundtrack_dic = {"The Bodyguard":"1992", "Saturday Night Fever":"1977"}
soundtrack_dic
{'The Bodyguard': '1992', 'Saturday Night Fever': '1977'}
a) In the dictionary soundtrack_dic what are the keys ?
# Write your code below and press Shift+Enter to execute
soundtrack_dic.keys()
dict_keys(['The Bodyguard', 'Saturday Night Fever'])
Click here for the solution
b) In the dictionary soundtrack_dic what are the values ?
# Write your code below and press Shift+Enter to execute
soundtrack_dic.values()
dict_values(['1992', '1977'])
Click here for the solution
You will need this dictionary for the following questions:
The Albums Back in Black, The Bodyguard and Thriller have the following music recording sales in millions 50, 50 and 65 respectively:
a) Create a dictionary album_sales_dict where the keys are the album name and the sales in millions are the values.
# Write your code below and press Shift+Enter to execute
album_sales_dict = {'Back in Black' : 50, 'The Bodyguard' : 50, 'Thriller' : 65}
Click here for the solution
album_sales_dict = {"The Bodyguard":50, "Back in Black":50, "Thriller":65}
b) Use the dictionary to find the total sales of Thriller:
Thriller
# Write your code below and press Shift+Enter to execute
album_sales_dict['Thriller']
65
Click here for the solution
c) Find the names of the albums from the dictionary using the method keys():
# Write your code below and press Shift+Enter to execute
album_sales_dict.keys()
dict_keys(['Back in Black', 'The Bodyguard', 'Thriller'])
Click here for the solution
album_sales_dict.keys()
d) Find the values of the recording sales from the dictionary using the method values:
# Write your code below and press Shift+Enter to execute
album_sales_dict.values()
dict_values([50, 50, 65])
Click here for the solution
album_sales_dict.values()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment