Python Tutorial: Containers – Lists and Dictionaries

Want to learn more? Take the full course at at your own pace. More than a video, you’ll learn hands-on coding & quickly apply skills to your daily work.

We will now talk about containers: data structures in which you can store multiple data types.

Lists are a very common data structure in Python that can contain multiple data types.

Lists in Python are created with a pair of square brackets, and each element in the list is separated by a comma.

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.

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.

Another difference in Python is using negative indices to select elements from the end.

So in Python, -1 selects the last element in the list, -2 selects the penultimate element in the list and so on.

Before we extract multiple list elements at once, it’s important to show the left inclusive, right exclusive nature of Python.

The best way to see this is by 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.

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.

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.

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.

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 is 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.

Finally, you can find the length, that is the number of elements, of a list and dictionary by using the built-in len() function.

Time to practice lists and dictionaries.

Post Author: hatefull