{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
\n",
"\n",
"Created by [Nathan Kelber](http://nkelber.com) and Ted Lawless for [JSTOR Labs](https://labs.jstor.org/) under [Creative Commons CC BY License](https://creativecommons.org/licenses/by/4.0/)
\n",
"For questions/comments/improvements, email nathan.kelber@ithaka.org.
\n",
"___"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Python Basics 3\n",
"\n",
"**Description:** This lesson describes the basics of [lists](https://docs.constellate.org/key-terms/#list) and [dictionaries](https://docs.constellate.org/key-terms/#dictionary) including:\n",
"\n",
"* The `in` and `not in` [operators](https://docs.constellate.org/key-terms/#operator)\n",
"* [Lists](https://docs.constellate.org/key-terms/#list)\n",
"* [List](https://docs.constellate.org/key-terms/#list) methods (`index()`, `append()`, `insert()`, `sort()`)\n",
"* [Dictionaries](https://docs.constellate.org/key-terms/#dictionary)\n",
"* [Dictionary](https://docs.constellate.org/key-terms/#dictionary) methods (`update()`, `keys()`, `values()`, `items()`, `get()`)\n",
"\n",
"This is part 3 of 3 in the series *Python Basics* that will prepare you to do text analysis using the [Python](https://docs.constellate.org/key-terms/#python) programming language.\n",
"\n",
"**Use Case:** For Learners (Detailed explanation, not ideal for researchers)\n",
"\n",
"**Difficulty:** Beginner\n",
"\n",
"**Completion Time:** 90 minutes\n",
"\n",
"**Knowledge Required:** \n",
"\n",
"* [Getting Started with Jupyter Notebooks](./getting-started-with-jupyter.ipynb)\n",
"* [Python Basics 1](./python-basics-1.ipynb)\n",
"* [Python Basics 2](./python-basics-2.ipynb)\n",
"\n",
"**Knowledge Recommended:** None\n",
"\n",
"**Data Format:** None\n",
"\n",
"**Libraries Used:** None\n",
"\n",
"**Research Pipeline:** None\n",
"___"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In *Python Basics 1*, we learned about three data types: [integers](https://docs.constellate.org/key-terms/#integer), [floats](https://docs.constellate.org/key-terms/#float), and [strings](https://docs.constellate.org/key-terms/#string). In this lesson, we will learn about two additional data types: [lists](https://docs.constellate.org/key-terms/#list) and [dictionaries](https://docs.constellate.org/key-terms/#dictionary). [Lists](https://docs.constellate.org/key-terms/#list) and [dictionaries](https://docs.constellate.org/key-terms/#dictionary) help us store many values inside a single [variable](https://docs.constellate.org/key-terms/#variable). This is helpful for a few reasons.\n",
"\n",
"* We can store many items in a single [list](https://docs.constellate.org/key-terms/#list) or [dictionary](https://docs.constellate.org/key-terms/#dictionary), making it easier to keep the data together\n",
"* [Lists](https://docs.constellate.org/key-terms/#list) and [dictionaries](https://docs.constellate.org/key-terms/#dictionary) only require a single [assignment state](https://docs.constellate.org/key-terms/#assignment-statement)\n",
"* [Lists](https://docs.constellate.org/key-terms/#list) and [dictionaries](https://docs.constellate.org/key-terms/#dictionary) have additional capabilities that will make organizing our data easier\n",
"\n",
"The fundamental difference between a [list](https://docs.constellate.org/key-terms/#list) and a [dictionary](https://docs.constellate.org/key-terms/#dictionary) is that a [list](https://docs.constellate.org/key-terms/#list) stores items in sequential order (starting from 0) while a [dictionary](https://docs.constellate.org/key-terms/#dictionary) stores items in [key/value pairs](https://docs.constellate.org/key-terms/#key-value-pair). When we want to retrieve an item in a list, we use an [index number](https://docs.constellate.org/key-terms/#index-number) or a set of [index numbers](https://docs.constellate.org/key-terms/#index-number) called a [slice](https://docs.constellate.org/key-terms/#slice) as a reference. When we want to retrieve an item from a [dictionary](https://docs.constellate.org/key-terms/#dictionary), we supply a [key](https://docs.constellate.org/key-terms/#key-value-pair) that returns the [value](https://docs.constellate.org/key-terms/#key-value-pair) (or set of values) associated with that [key](https://docs.constellate.org/key-terms/#key-value-pair). Each of these approaches can be beneficial depending on what kind of data we are working with (and what we intend to do with the data)."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Lists\n",
"\n",
"A [list](https://docs.constellate.org/key-terms/#list) can store anywhere from zero to millions of items. The items that can be stored in a [list](https://docs.constellate.org/key-terms/#list) include the data types we have already learned: [integers](https://docs.constellate.org/key-terms/#integer), [floats](https://docs.constellate.org/key-terms/#float), and [strings](https://docs.constellate.org/key-terms/#string). A [list](https://docs.constellate.org/key-terms/#list) [assignment state](https://docs.constellate.org/key-terms/#assignment-statement) takes the form.\n",
"`my_list = [item1, item2, item3, item4...]`"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# A list containing integers\n",
"my_favorite_numbers = [7, 21, 100]\n",
"print(my_favorite_numbers)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# A list containing strings\n",
"my_inspirations = ['Harriet Tubman', 'Rosa Parks', 'Pauli Murray']\n",
"print(my_inspirations)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Both `my_favorite_numbers` and `my_inspirations` have three items, but we could have also initialized them with no items `my_favorite_numbers = []` or many more items. Each item has an [index number](https://docs.constellate.org/key-terms/#index-number) that depends on their order. The first item is 0, the second item is 1, the third item is 2, etc. In the `my_inspirations` list, `'Pauli Murray'` is item 2."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Retrieving an item in a list\n",
"my_inspirations[2]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"___\n",
"