Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

lists

The list data type is perhaps the most common data type for working with multiple data. Textual data tends to be saved as a list, or a collection of separate strings, each string consisting of a sequence of characters that represents a single word.

It is important to remember that while humans might read each string as a word, the computer does not ascribe any semantic value to it, only processing it as sequence of characters.

list indexing

List indexing enables programmers access specific items in a list. It is super useful for working with large lists, such as a full text, or even a collection of texts. It works by accessing items according to their position within a list.

By “position,” I mean where that item is located on a list. The moment a list is created in Python, it comes with an implicit index, where each item is assigned a numerical position based on its location. The first item is assigned to the position 0, the second item is assigned 1, the third item is assigned 2, the fourth item assigned 3, and so on.

The syntax for list indexing is to declare the list name followed by brackets that contain the index. The first item in a list would be accessed with the index 0.

Advanced question: Why does list indexing begin with 0?

The answer is a bit complicated, and gets into technical details of how computer memory works in programming. Put simply, it has to do with the memory location being based in offsets. Because the first item of a list is also the origin location of the list, its offset is 0. In other words, to go to the first item in the list, there is no movement (or offset) from the current, or origin, location. The second item of the list requires 1 movement from current location, and is therefore assigned to the location 1.

The third item in a list would be accessed with the index 2.

Python offers a seemingly strange syntax for accessing the last item of a list, which you can use to take a peek at the end of a list. The last item in a list is accessed with the index -1. This might seem peculiar, but if you imagine the list items forming a circle which goes clockwise, then going backward (counterclockwise) one step would take you to the last item. That backward step is represented with -1.

list slicing

Like list indexing, list slicing uses a list's implicit index to access items. Unlike list indexing, however, slicing enables someone to access a range, or a "slice," of items from a list. This capability becomes convenient when working with large lists. For example, we might take a look into a section of a list without loading the entire list, or we might grab a portion of that list to create a new list.

To get a slice, you need to enter the starting and ending point of the section which you want to grab. In between these values, insert a colon, which represents every item that occurs in between the start and end points. A list slice therefore looks like the following:

The syntax for list slicing is a bit wonky, but like list indexing, it is just a matter of getting used to the particulars of Python’s way of counting by starting with zero, as well as the rules for inclusive and exclusive values.

Here’s the tricky part: the starting point is always inclusive, and the ending point is always exclusive. Which means that the slice will take everything starting at position 2, up until, but exclusing, the item in position 5. This rule for list slicing can cause some confusion, and it’s best just to memorize it.

One more important thing to know is the colon. In its position between the start and end point, it signifies everything in between. But if there is nothing before or after the colon, then the colon represents everything until the beginning or end of the list. For example, the syntax [1:] will take everything starting at index 1 (or the second item) until the end of the list. Similarly, [:4] will take everything from the start until the fourth index position of the list.

This trick with the colon can be really nifty for grabbing the first 10 or first 100 items from a list, allowing you to take peeks into the list items without printing the entire list. We will use this trick a lot in future workshops.

list methods

Like strings, lists also have specific methods that only work with list type data. List methods enable one to do various things to data contained in lists. Some of the more popular methods have to do with creating and modifying items in a list.

One list method, append(), adds items to a list. For example, to a list of breakfast items, one can add additional yummy items using append():

Other methods, remove() and pop() take items out from a list. While remove() takes out a specific item (declared within the parentheses), pop() will only take out the last item in a list, "popping" it off, so to speak.

Besides adding and deleting items, we can also sort items in a list. For these, we use the sort() and reverse() methods.

To show the power of these methods, we'll work with a larger list, like a list of words from this chapter's description. First, we will copy and paste the workshop description as a single string. Then, we will split the string, saving the output to a new variable. Once the data is in list format, we will run the sort() and reverse() methods.

Notice the syntax wrapping the string here, the triple quotation marks (instead of single), for saving the string. The triple quotes enables our strings to break lines and include other punctuation without messing with the string format.

Notice a couple of things:

  1. I lied. Well, I omitted something. I included a string method when I said I was only going to show you list methods. Can you spot the rogue string method? Hint: you've seen it before, and it is attached to a string type of object.

  2. Some methods return new objects, while other methods simply change existing ones. The sort() method is one that changes the existing object. Unlike the split() method, we don't have to save the results of sort() to a new variable. sort(), and other methods like reverse(), change the item “in place,” meaning that the existing list is modified. split(), by contrast, creates a new object, which needs to be saved. This concept of changing things in place is a bit advanced, so there is no rush to grasp it now.

  3. The resulting list is organized alphabetically, which means that any punctuation and/or numbers will appear before any letters. This will explain why the results may not seem alphabetically sorted at first.

  4. See the little hashtag # symbol? That’s how you write a comment in Python. By putting the # at the start of the line, that tells Pyhon not to process that line’s contents, that it’s meant for human readers.

Remember that these list methods will not work on string type data. Try running a few of them on a string, just to see what happens.

The new concepts you learned in this lesson (list methods, indexing, and slicing) will be crucial tools for working with lists in future lessons and workshops. You will have plenty of opportunities to revisit the concepts over and over again until they start to seem more intuitive.

In the next lessons, we will introduce skills for working more programmatically with lists.