"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Dictionaries\n",
"\n",
"We've been learning about *sequences* in Python but now we're going to switch gears and learn about *mappings* in Python. If you're familiar with other languages you can think of these Dictionaries as hash tables. \n",
"\n",
"This section will serve as a brief introduction to dictionaries and consist of:\n",
"\n",
" 1.) Constructing a Dictionary\n",
" 2.) Accessing objects from a dictionary\n",
" 3.) Nesting Dictionaries\n",
" 4.) Basic Dictionary Methods\n",
"\n",
"So what are mappings? Mappings are a collection of objects that are stored by a *key*, unlike a sequence that stored objects by their relative position. This is an important distinction, since mappings won't retain order since they have objects defined by a key.\n",
"\n",
"A Python dictionary consists of a key and then an associated value. That value can be almost any Python object.\n",
"\n",
"\n",
"## Constructing a Dictionary\n",
"Let's see how we can construct dictionaries to get a better understanding of how they work!"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# Make a dictionary with {} and : to signify a key and a value\n",
"my_dict = {'key1':'value1','key2':'value2'}"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'value2'"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Call values by their key\n",
"my_dict['key2']"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Its important to note that dictionaries are very flexible in the data types they can hold. For example:"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"my_dict = {'key1':123,'key2':[12,23,33],'key3':['item0','item1','item2']}"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['item0', 'item1', 'item2']"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Let's call items from the dictionary\n",
"my_dict['key3']"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'item0'"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Can call an index on that value\n",
"my_dict['key3'][0]"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'ITEM0'"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Can then even call methods on that value\n",
"my_dict['key3'][0].upper()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can affect the values of a key as well. For instance:"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"123"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"my_dict['key1']"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# Subtract 123 from the value\n",
"my_dict['key1'] = my_dict['key1'] - 123"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#Check\n",
"my_dict['key1']"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"A quick note, Python has a built-in method of doing a self subtraction or addition (or multiplication or division). We could have also used += or -= for the above statement. For example:"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"-123"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Set the object equal to itself minus 123 \n",
"my_dict['key1'] -= 123\n",
"my_dict['key1']"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can also create keys by assignment. For instance if we started off with an empty dictionary, we could continually add to it:"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# Create a new dictionary\n",
"d = {}"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# Create a new key through assignment\n",
"d['animal'] = 'Dog'"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# Can do this with any object\n",
"d['answer'] = 42"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'animal': 'Dog', 'answer': 42}"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#Show\n",
"d"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Nesting with Dictionaries\n",
"\n",
"Hopefully you're starting to see how powerful Python is with its flexibility of nesting objects and calling methods on them. Let's see a dictionary nested inside a dictionary:"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# Dictionary nested inside a dictionary nested inside a dictionary\n",
"d = {'key1':{'nestkey':{'subnestkey':'value'}}}"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Wow! That's a quite the inception of dictionaries! Let's see how we can grab that value:"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'value'"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Keep calling the keys\n",
"d['key1']['nestkey']['subnestkey']"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## A few Dictionary Methods\n",
"\n",
"There are a few methods we can call on a dictionary. Let's get a quick introduction to a few of them:"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# Create a typical dictionary\n",
"d = {'key1':1,'key2':2,'key3':3}"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"dict_keys(['key1', 'key2', 'key3'])"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Method to return a list of all keys \n",
"d.keys()"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"dict_values([1, 2, 3])"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Method to grab all values\n",
"d.values()"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"dict_items([('key1', 1), ('key2', 2), ('key3', 3)])"
]
},
"execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Method to return tuples of all items (we'll learn about tuples soon)\n",
"d.items()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Hopefully you now have a good basic understanding how to construct dictionaries. There's a lot more to go into here, but we will revisit dictionaries at later time. After this section all you need to know is how to create a dictionary and how to retrieve values from it."
]
}
],
"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.6.6"
}
},
"nbformat": 4,
"nbformat_minor": 1
}