Oftentimes when programming, you're going to need a way to organize and manage sets of data. This is no different than in real life. If you're putting together an invite list for a party; or creating a shopping list; or even just a basic To Do list. In each of these situations, organizing data in a list format makes the creating, sorting, managing and finding of the information easier and faster.
Most programming languages have concepts of lists as well, and Python is no exception. Using the Python programming language, you can create many types of lists, though the two most common types are lists of strings and list of numbers. In general, a list is initially assigned to a variable and that variable can then be used to add to, sort, modify or recall one or more items from the list.
An example of a list of strings would be a list containing the names of the months of the year. To keep things simple, let’s just create a list containing the first five months; we’ll use a variable called “months”:
Notice how the entire list has brackets [ ] around it and how each of the items in the list are strings with quotation marks around them.
Now let’s look at some of the things we can do with that list…
We can reference the entire list. For example, let’s say we wanted to print the whole thing out:
We can reference a single item in the list. For example, let’s say we wanted to print out the third item in the list (which is, “March”):
Wait! I said we were going to print out the third item on the list, and then I had it print months[2] – that’s not a typo. Computers always start counting lists at 0, not 1. So, the first item on a list is [0], the second item on a list is [1] and so on. To get the third item on the item, we reference it as [2].
Here is how the computer sees our list:
ITEM # |
ITEM NAME |
0 |
January |
1 |
February |
2 |
March |
3 |
April |
4 |
May |
So, the third item on our list of months is months[2].
We can reference a range of items on the list. For example, let’s say we wanted to print out items 2 and 3 in the list (February, March):
Notice that we referenced [1:3] to get items 1 and 2. When we reference a range, the second number is the item where we stop – that item is not included in the range.
We can change an item in the list. For example, let’s say we wanted to change “February” to “Feb” to make it shorter (and then print it out to verify that it has been changed):
We can add an item to the list. For example, if we decide that we want to include June at the end of the list, we can do the following:
The .append() that we used above is called a function. We’ll discuss later how and why this works. For now, just be aware that there are bunch of ways you can act on a list (or any variable, for that matter) using what are known as functions.
Just like we added an item to the list above, we can also remove an item from the list. For example, if we decide that we don’t like April, we can get rid of it from the list:
After making all of these changes, here is how the computer would now see this list:
ITEM # |
ITEM NAME |
0 |
January |
1 |
Feb |
2 |
March |
3 |
May |
4 |
June |