# LISTS

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. As an example, let's consider a list of strings 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":

months = ['January', 'February', 'March', 'April', 'May']

Notice how the entire list has brackets `[]` around it. Each of the items in the list are strings and they are separated by commas.

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:

  print(months)

- 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"):

  print(months[2])

  Wait! We said we were going to print out the third item on the list, and then we 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 find the length of the list with the built-in `len()` function. For example:

  print(len(months))

- 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):

  print(months[1:3])

  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):

  months[1] = 'Feb'

- 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:

  months.append('June')

  The .append() that we used above is called a function — see INTRO TO FUNCTIONS for more information.

- 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:

  months.remove('April')

- 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