Skip to content

Instantly share code, notes, and snippets.

@salamituns
Created January 29, 2021 12:37
Show Gist options
  • Save salamituns/1e86bab9807c8de5bebd00ecb12fd6e8 to your computer and use it in GitHub Desktop.
Save salamituns/1e86bab9807c8de5bebd00ecb12fd6e8 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": [
" Dictionary. {}\n",
"A dictionary consists of keys and values\n",
"These keys are the keys that are used to access values within a dictionary.\n",
"Each key is separated from its value by a colon \":\". \n",
"\n",
"Keys can only be strings, numbers, or tuples, but values can be any data type.\n",
"\n",
"Commas separate the items, and the whole dictionary is enclosed in curly braces.\n",
"\n",
"And we can compare Dictionary to a list.\n",
"But unlike List that uses index to access value, we use Keys to access values in dictionary.\n",
"\n",
"And where index starts at 0, keys starts at 1."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'key1': 1,\n",
" 'key2': '2',\n",
" 'key3': [3, 3, 3],\n",
" 'key4': (4, 4, 4),\n",
" 'key5': 5,\n",
" (0, 1): 6}"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#Example of a dictionary\n",
"\n",
"Dict = {\"key1\": 1, \"key2\": \"2\", \"key3\": [3, 3, 3], \"key4\": (4, 4, 4), ('key5'): 5, (0, 1): 6}\n",
"Dict"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#Accessing the value in a key.\n",
"Dict[\"key1\"]"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'Thriller': '1982',\n",
" 'Back in Black': '1980',\n",
" 'The Dark Side of the Moon': '1973',\n",
" 'The Bodyguard': '1992',\n",
" 'Bat Out of Hell': '1977',\n",
" 'Their Greatest Hits (1971-1975)': '1976',\n",
" 'Saturday Night Fever': '1977',\n",
" 'Rumours': '1977'}"
]
},
"execution_count": 3,
"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": [
"For every key, there can only be one single value, however, multiple keys can hold the same value."
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'1982'"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#We can value from a key by using this syntax.\n",
"release_year_dict['Thriller'] "
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"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'])"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#We can get all the keys in a dictionary usinf the following syntax.\n",
"release_year_dict.keys()"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"dict_values(['1982', '1980', '1973', '1992', '1977', '1976', '1977', '1977'])"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#Likewise the same thing for values.\n",
"release_year_dict.values()"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'Thriller': '1982',\n",
" 'Back in Black': '1980',\n",
" 'The Dark Side of the Moon': '1973',\n",
" 'The Bodyguard': '1992',\n",
" 'Bat Out of Hell': '1977',\n",
" 'Their Greatest Hits (1971-1975)': '1976',\n",
" 'Saturday Night Fever': '1977',\n",
" 'Rumours': '1977',\n",
" 'Graduation': '2007'}"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#Appending a new value and key into the dictionary using this syntax format\n",
"\n",
"release_year_dict['Graduation'] = '2007'\n",
"release_year_dict"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'Back in Black': '1980',\n",
" 'The Dark Side of the Moon': '1973',\n",
" 'The Bodyguard': '1992',\n",
" 'Bat Out of Hell': '1977',\n",
" 'Their Greatest Hits (1971-1975)': '1976',\n",
" 'Saturday Night Fever': '1977',\n",
" 'Rumours': '1977'}"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#delete entries by keys\n",
"del(release_year_dict['Thriller'])\n",
"del(release_year_dict['Graduation'])\n",
"release_year_dict"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#We can verify if an element is in the dictionary\n",
"'The Bodyguard' in release_year_dict"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'The Bodyguard': '1992', 'Saturday Night Fever': '1977'}"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"soundtrack_dic = {\"The Bodyguard\":\"1992\", \"Saturday Night Fever\":\"1977\"}\n",
"soundtrack_dic "
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"dict_keys(['The Bodyguard', 'Saturday Night Fever'])"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"soundtrack_dic.keys()"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"dict_values(['1992', '1977'])"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"soundtrack_dic.values()"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'Back in Black': '50', 'The Bodyguard': '50', 'Thriller': '65'}"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#A dictionary of album and the recording sales respectively.\n",
"album_sales_dict = {'Back in Black': '50', 'The Bodyguard': '50', 'Thriller': '65'}\n",
"album_sales_dict"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'65'"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#To find the album sales of Thriller\n",
"album_sales_dict['Thriller']"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"dict_keys(['Back in Black', 'The Bodyguard', 'Thriller'])"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#To find the names of the albums from the dictionary usinf the .keys function\n",
"album_sales_dict.keys()"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"dict_values(['50', '50', '65'])"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#Finding the values of the recording sales from the dictionary\n",
"album_sales_dict.values()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python",
"language": "python",
"name": "conda-env-python-py"
},
"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.6.12"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment