Skip to content

Instantly share code, notes, and snippets.

@emilfh
Created January 4, 2021 17:42
Show Gist options
  • Save emilfh/8ebf5eac54959846748bcf98f3c8516f to your computer and use it in GitHub Desktop.
Save emilfh/8ebf5eac54959846748bcf98f3c8516f to your computer and use it in GitHub Desktop.
Created on Skills Network Labs
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
" <a href=\"http://cocl.us/topNotebooksPython101Coursera\"><img src = \"https://ibm.box.com/shared/static/yfe6h4az47ktg2mm9h05wby2n7e8kei3.png\" width = 750, align = \"center\"></a>\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" <a href=\"https://www.bigdatauniversity.com\"><img src = \"https://ibm.box.com/shared/static/ugcqz6ohbvff804xp84y4kqnvvk3bq1g.png\" width = 300, align = \"center\"></a>\n",
"\n",
"<h1 align=center><font size = 5>Sets and Dictionaries</font></h1>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"## Table of Contents\n",
"\n",
"\n",
"<div class=\"alert alert-block alert-info\" style=\"margin-top: 20px\">\n",
"\n",
"<li><a href=\"#ref2\">Dictionaries</a></li>\n",
"<br>\n",
"<p></p>\n",
"Estimated Time Needed: <strong>20 min</strong>\n",
"</div>\n",
"\n",
"<hr>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<a id=\"ref2\"></a>\n",
"<h2 align=center> Dictionaries in Python </h2>\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"A dictionary consists of keys and values. It is helpful to compare a dictionary to a list. Instead of the numerical indexes such as a list, dictionaries have keys. These keys are labels that are used to access values within a dictionary."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<a ><img src = \"https://ibm.box.com/shared/static/6tyznuwydogmtuv73o8l5g7xsb8o92h2.png\" width = 650, align = \"center\"></a>\n",
" <h4 align=center> A Comparison of a Dictionary to a list: Instead of the numerical indexes like a list, dictionaries have keys.\n",
" </h4> \n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" An example of a Dictionary **Dict**:"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"{'key2': '2',\n",
" 'key5': 5,\n",
" 'key1': 1,\n",
" (0, 1): 6,\n",
" 'key3': [3, 3, 3],\n",
" 'key4': (4, 4, 4)}"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"Dict={\"key1\":1,\"key2\":\"2\",\"key3\":[3,3,3],\"key4\":(4,4,4),('key5'):5,(0,1):6}\n",
"Dict"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" The keys can be strings:"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"1"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"Dict[\"key1\"]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" Keys can also be any immutable object such as a tuple: "
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"6"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"Dict[(0,1)]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" 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 \"**{}**\"."
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"{'Back in Black': '1980',\n",
" 'Bat Out of Hell': '1977',\n",
" 'Rumours': '1977',\n",
" 'Saturday Night Fever': '1977',\n",
" 'The Bodyguard': '1992',\n",
" 'The Dark Side of the Moon': '1973',\n",
" 'Their Greatest Hits (1971-1975)': '1976',\n",
" 'Thriller': '1982'}"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"release_year_dict = {\"Thriller\":\"1982\", \"Back in Black\":\"1980\", \\\n",
" \"The Dark Side of the Moon\":\"1973\", \"The Bodyguard\":\"1992\", \\\n",
" \"Bat Out of Hell\":\"1977\", \"Their Greatest Hits (1971-1975)\":\"1976\", \\\n",
" \"Saturday Night Fever\":\"1977\", \"Rumours\":\"1977\"}\n",
"release_year_dict"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"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 a 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."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"It is helpful to visualize the dictionary as a table, as in figure 9. The first column represents the keys, the second column represents the values.\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" <a ><img src = \"https://ibm.box.com/shared/static/i45fppou18c3t0fuf2ikks48tod7chbl.png\" width = 650, align = \"center\"></a>\n",
" <h4 align=center> Figure 9: Table representing a Dictionary \n",
"\n",
" </h4> \n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You will need this dictionary for the next two questions :"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"{'Saturday Night Fever': '1977', 'The Bodyguard': '1992'}"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"soundtrack_dic = { \"The Bodyguard\":\"1992\", \"Saturday Night Fever\":\"1977\"}\n",
"soundtrack_dic "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### In the dictionary \"soundtrack_dict\" what are the keys ?"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": true
},
"source": [
"'Saturday Night Fever' and 'The Bodyguard'"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" <div align=\"right\">\n",
"<a href=\"#Dict1\" class=\"btn btn-default\" data-toggle=\"collapse\">Click here for the solution</a>\n",
"\n",
"</div>\n",
"<div id=\"Dict1\" class=\"collapse\">\n",
"```\n",
"The Keys \"The Bodyguard\" and \"Saturday Night Fever\" \n",
"```\n",
"</div>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### In the dictionary \"soundtrack_dict\" what are the values ?"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"'1977' and '1992"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" <div align=\"right\">\n",
"<a href=\"#Dict2\" class=\"btn btn-default\" data-toggle=\"collapse\">Click here for the solution</a>\n",
"\n",
"</div>\n",
"<div id=\"Dict2\" class=\"collapse\">\n",
"```\n",
"The values are \"1992\" and \"1977\"\n",
"```\n",
"</div>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You can retrieve the values based on the names:"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"'1982'"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"release_year_dict['Thriller'] "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This corresponds to: \n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" <a ><img src = \"https://ibm.box.com/shared/static/glbwz23cgjjxqi7rjxn7me5i16gan7h7.png\" width = 500, align = \"center\"></a>\n",
" <h4 align=center> \n",
" Table used to represent accessing the value for \"Thriller\" \n",
"\n",
" </h4> \n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Similarly for The Bodyguard \n"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"'1992'"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"release_year_dict['The Bodyguard'] "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" <a ><img src = \"https://ibm.box.com/shared/static/6t7bu8jusckaskukwq1k0a3im5ltcpsn.png \" width = 500, align = \"center\"></a>\n",
" <h4 align=center> \n",
"Accessing the value for the \"The Bodyguard\"\n",
"\n",
" </h4> \n",
"\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now let us retrieve the keys of the dictionary using the method **`keys()`**:"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"dict_keys(['The Dark Side of the Moon', 'Thriller', 'Bat Out of Hell', 'Rumours', 'Their Greatest Hits (1971-1975)', 'Saturday Night Fever', 'The Bodyguard', 'Back in Black'])"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"release_year_dict.keys() "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" You can retrieve the values using the method **`values()`**:"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"dict_values(['1973', '1982', '1977', '1977', '1976', '1977', '1992', '1980'])"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"release_year_dict.values() "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can add an entry:"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"{'Back in Black': '1980',\n",
" 'Bat Out of Hell': '1977',\n",
" 'Graduation': '2007',\n",
" 'Rumours': '1977',\n",
" 'Saturday Night Fever': '1977',\n",
" 'The Bodyguard': '1992',\n",
" 'The Dark Side of the Moon': '1973',\n",
" 'Their Greatest Hits (1971-1975)': '1976',\n",
" 'Thriller': '1982'}"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"release_year_dict['Graduation']='2007'\n",
"release_year_dict"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can delete an entry: "
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"{'Back in Black': '1980',\n",
" 'Bat Out of Hell': '1977',\n",
" 'Rumours': '1977',\n",
" 'Saturday Night Fever': '1977',\n",
" 'The Bodyguard': '1992',\n",
" 'The Dark Side of the Moon': '1973',\n",
" 'Their Greatest Hits (1971-1975)': '1976'}"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"del(release_year_dict['Thriller'])\n",
"del(release_year_dict['Graduation'])\n",
"release_year_dict"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" We can verify if an element is in the dictionary: "
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"'The Bodyguard' in release_year_dict"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### The Albums 'Back in Black', 'The Bodyguard' and 'Thriller' have the following music recording sales in millions 50, 50 and 65 respectively:"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"##### a) Create a dictionary “album_sales_dict” where the keys are the album name and the sales in millions are the values. "
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"{'Back in Black': 50, 'The Bodyguard': 50, 'Thriller': 60}"
]
},
"execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"album_sales_dict={'Back in Black':50,'The Bodyguard':50,'Thriller':60}\n",
"album_sales_dict"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<div align=\"right\">\n",
"<a href=\"#q9\" class=\"btn btn-default\" data-toggle=\"collapse\">Click here for the solution</a>\n",
"\n",
"</div>\n",
"<div id=\"q9\" class=\"collapse\">\n",
"```\n",
"album_sales_dict= { \"The Bodyguard\":50, \"Back in Black\":50,\"Thriller\":65}\n",
"```\n",
"</div>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### b) Use the dictionary to find the total sales of \"Thriller\":"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"60"
]
},
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"album_sales_dict['Thriller']"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<div align=\"right\">\n",
"<a href=\"#q10b\" class=\"btn btn-default\" data-toggle=\"collapse\">Click here for the solution</a>\n",
"\n",
"</div>\n",
"<div id=\"q10b\" class=\"collapse\">\n",
"```\n",
"album_sales_dict[\"Thriller\"]\n",
"```\n",
"</div>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### c) Find the names of the albums from the dictionary using the method \"keys\":"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"dict_keys(['Thriller', 'The Bodyguard', 'Back in Black'])"
]
},
"execution_count": 25,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"album_sales_dict.keys()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<div align=\"right\">\n",
"<a href=\"#q10c\" class=\"btn btn-default\" data-toggle=\"collapse\">Click here for the solution</a>\n",
"\n",
"</div>\n",
"<div id=\"q10c\" class=\"collapse\">\n",
"```\n",
"album_sales_dict.keys()\n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### d) Find the names of the recording sales​ from the dictionary using the method \"values\":"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"dict_values([60, 50, 50])"
]
},
"execution_count": 26,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"album_sales_dict.values()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<div align=\"right\">\n",
"<a href=\"#q10d\" class=\"btn btn-default\" data-toggle=\"collapse\">Click here for the solution</a>\n",
"\n",
"</div>\n",
"<div id=\"q10d\" class=\"collapse\">\n",
"```\n",
"album_sales_dict.values()\n",
"```\n",
"</div>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<a href=\"http://cocl.us/bottemNotebooksPython101Coursera\"><img src = \"https://ibm.box.com/shared/static/irypdxea2q4th88zu1o1tsd06dya10go.png\" width = 750, align = \"center\"></a>\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"\n",
"# About the Authors: \n",
"\n",
" [Joseph Santarcangelo]( https://www.linkedin.com/in/joseph-s-50398b136/) has a PhD in Electrical Engineering, his research focused on using machine learning, signal processing, and computer vision to determine how videos impact human cognition. Joseph has been working for IBM since he completed his PhD.\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" <hr>\n",
"Copyright &copy; 2017 [cognitiveclass.ai](cognitiveclass.ai?utm_source=bducopyrightlink&utm_medium=dswb&utm_campaign=bdu). This notebook and its source code are released under the terms of the [MIT License](https://bigdatauniversity.com/mit-license/)."
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.5.2"
},
"widgets": {
"state": {},
"version": "1.1.2"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment