Lists

Jump to TL;DR (summary)

Lists in python are very simple to create. They are specified by a square bracket [, a comma separated list of the data you would like, and a closing square bracket ]. Below are a few examples of lists.

MY_FIRST_LIST = [0, 1, 2]
MY_SECOND_LIST = ["Joe", "Fred", "Hubert"]

Lists alone are pretty cool, but we sometimes might want to get a single value from a list. To do so, we need to use some more square brackets. This time, our square brackets go after the name of the list, and within them is the index of the value we want.

If I had a list MY_LIST and wanted to get the 2nd element, I would write MY_LIST[1]. And yes, that is not a typo, you use the number 1 to refer to the 2nd element. Why is that? Well it has to do with a whole lot of how computers work, but the main thing to remember is that 1 is not the first element, 0 is.

Lists in python are what are called 0 indexed lists, or their first value has the index of 0

Here is another example of getting data from a list

FRUIT_WORDS = ["apple", "bananna", "cherry"]

print(FRUIT_WORDS[0]) # Prints "apple"
print(FRUIT_WORDS[1]) # Prints "bananna"
print(FRUIT_WORDS[2]) # Prints "cherry"

Mutation

Just having a list of items is cool, but what if we want to change what is in the list? Well that is where list mutation comes into play.

There are a few ways we can mutate a list. We can change the value stored in one of the indexes, like so

fruit_words = ["apple", "bananna", "cherry"]

FRUIT_WORDS[1] = "berries" # Changes "bananna" to "berries"

print(FRUIT_WORDS[1]) # Prints "berries"

Note how the list's name is lower_snake_case instead of UPPER_SNAKE_CASE since it is mutable or not constant

Or, we can add items to the end of the list, with the append method

my_list = ["a", "b", "c"]
my_list.append("d")
print(my_list) # Prints "['a', 'b', 'c', 'd']"

print can print out whole lists for us which is useful for making sure the list has what we think it has in it

As well as adding items to lists, we can remove items.

my_list.remove("b")
print(my_list) # Prints "['a', 'c', 'd']"

WATCH OUT remove takes in the value to remove, not the index

Length

We all know the size of your list is not what matters, but instead how you use it. Sometimes it is useful to know its length every once and a while though. Luckily for us, python provides a handy dandy len function, the same one we have been using to get the length of a string.

FRUIT_WORDS = ["apple", "bananna", "cherry"]

print(len(FRUIT_WORDS)) # Prints "3"

Looping

Sometimes, just getting one element is not enough for us, and instead we want to do something with or to the whole list. To do so, we can use a loop to go over each element of the list, and do something. The simplest thing to do is to just print out each element in a list.

FRUIT_WORDS = ["apple", "bananna", "cherry"]

for fruit in FRUIT_WORDS:
    print(fruit) # Will print each name in the list

We might also want to produce a single value based on all the items in the list, such as a sum. Let's make a function to calculate the sum of a list of numbers. Its signature would look as such:

# sum : ListOfNumbers -> Number
# Sums up all the numbers in a list of numbers
def sum(list_of_numbers):

Since we need a place to store our sum, we will create a variable and call it result

    result = 0 # We start with a sum of 0

And then we can loop over every element

    for n in list_of_numbers: # Loop over every number `n` in the list
        result += n # Add the current number to the sum

And then finally, we can return the result, leaving us with

# sum : ListOfNumbers -> Number
# Sums up all the numbers in a list of numbers
def sum(list_of_numbers):
    result = 0 # We start with a sum of 0

    for n in list_of_numbers: # Loop over every number `n` in the list
        result += n # Add the current number to the sum

    # Note the un-indentation, since this return is outside of the for loop
    # If the return was inside of the for loop, the function would return the
    # sum after only adding the first value, basically returning the first value
    return result # Give the result to the caller

If we were to use our function, we could do so as such:

sum([0, 2, 3, 4]) # = 9
sum([6]) # = 6
sum([15, 21, 55]) # = 91

Example: Counting

# count_big_strings : ListOfStrings -> Nat
# Count the number of strings that have at least 10 letters
def count_big_strings(list_of_strings):
    count = 0 # We have 0 big strings by default

    for s in list_of_strings: # Loop over every string `s` in the list
        if len(s) >= 10: # This will check if the condition is true
            count += 1 # And run this code if it is true, hence the name
    
    return count # Return the result, outside of the for and if statement

Too Long; Didn't Read

If you want an abridged version, you can look at just the code that I have gone over here