1. Containers - lists and dictionaries
We will now talk about containers: data structures in which you can store multiple data types.
2. Lists
Lists are a very common data structure in Python that can contain multiple data types.
3. Creating lists
Lists in Python are created with a pair of square brackets,
and each element in the list is separated by a comma.
4. Python is 0-indexed
Python is a 0-indexed language, meaning that in Python,
the index is counted from 0 instead of 1.
This is one of the major sources of confusion when people switch between R and Python.
5. Subsetting lists
To extract an element out of a Python list, you use one pair of square brackets.
Since Python is a 0-indexed language, to get the first element, you use the index 0.
6. Negative indices
Another difference in Python is using negative indices to select elements from the end.
7. Negative indices
So in Python, -1 selects the last element in the list, -2 selects the penultimate element in the list and so on..
8. Left inclusive right exclusive
Before we extract multiple list elements at once, it's important to show the left inclusive, right exclusive nature of Python.
9. Think fence posts
The best way to see this is using a fence post analogy. Here the indices are labeled 0 to 5. These labels represent our "fence posts". Between each fence post you have elements of the list.
The elements between the indices 0 and 3 give you the first three elements of the list.
10. Slicing part 1
To simultaneously select multiple elements you use the colon notation. The first value represents the beginning index, and the second value represents the last index. You can also have an additional third value to specify a step size. So, if you want to select every other value from the first 5 elements in the list,
you would write 0 colon 5 colon 2.
11. Slicing part 2
The indices you pass into the colon notation can be optional. Remember the 0-index and the left inclusive right exclusive nature of Python? You are about to see this again. If you leave the first value blank, Python will slice from the beginning all the way to the second specified index. If you leave the second value missing then Python will slice from the first specified index until the end. You can also leave the first two values blank, and just specify a step size. This will go through the entire list and return the elements depending on the specified step size.
12. Dictionaries
Lists are great for storing information, but since the elements in the list can actually mean something,
a better, and more pythonic way is to associate a label with the elements. You cannot have named lists in Python. Instead, Python "dict"ionaries are one solution to this. Just like an actual dictionary, the data is stored in key-value pairs.
13. Python: Creating and working with dictionaries
Python dictionaries are created with a pair of curly-brackets. Each key-value pair includes a colon, with the key to the left, and the value to the right of the colon. Multiple key-value pairs are separated by commas.
Notice the order of the keys are different from when the dictionary was first defined, and when it was printed. The order of the keys are not guaranteed, so do not rely on the printed order of keys.
To extract a "value" from the dictionary, you place the "key" of the dictionary inside the square brackets.
14. Finding the length
Finally, you can find the length, that is the number of elements, of a list and dictionary by using the built-in len() function.
15. Let's practice!
Time to practice lists and dictionaries.