Lecture 09 Lists

Joseph Haugh

University of New Mexico

Free Recall

  • Get out a sheet of paper or open a text editor
  • For 2 minutes write down whatever comes to mind about the last class
    • This could be topics you learned
    • Questions you had
    • Connections you made

Start Reading

  • Let’s start by reading chapter 13
  • Take about 10-15 minutes to do this

Lists

  • Lists are a sequence of variables which are grouped under a single name
  • This allows us to handle an arbitrary number of variables in our programs
  • Creating a list is simple:
ls = [1,2,3]
print(ls)

Visualize

Accessing Elements of a List

  • Just like with string you can access specific elements of a list using square brackets
  • For example:
ls = [1,2,3]
print(ls[0])
print(ls[1])
print(ls[2])

Visualize

Modifying Elements of a List

  • Lists have an additional power overs strings, you can modify them
  • For example:
ls = [1,2,3]
ls[0] = 4
print(ls)

Visualize

We changed the first element, index 0, to be 4 instead of 1.

Modifying Elements of a List

  • You can also do this in a loop:
  • What is the output of the following code?
ls = [2,4,6,8]
for i in range(len(ls)):
    if i % 2 == 0:
        ls[i] = ls[i] * 2
print(ls)

Visualize

Notice that the ls[i] on the right hand side of the equal is retrieve the current value from the list which is then being used to update ls[i]. This is similar to doing x = x + 1.

Common Mistake: Index Out of Bounds

  • A common mistake to make is asking for an index which doesn’t exist in a given list
  • For example:
ls = [1,2,3]
#     0 1 2
print(ls[3]) # Too high

Visualize

Common Operations: len

  • As we saw before you can determine the length of a list using the len function
  • For example:
ls = [1,2,3,4,5]
length = len(ls)
print("length is", length)
for i in range(length):
    print("ls[", i, "] = ", ls[i])

Visualize

Concatenating Lists

  • Just like with strings we can concatenate lists using the + operator
  • For example:
ls1 = [1,2,3]
ls2 = [4,5,6,7]
ls3 = ls1 + ls2
print(ls3)

Visualize

Multiplying Lists

  • Python allows you to “multiply” lists
  • This creates a certain number of copies of the list and concatenates them together
  • This also works for strings!
  • For example:
ls1 = [1,2]
ls2 = ls1 * 5
print(ls2)

Visualize

Coding Exercise: It’s Natural

  • Let’s do this exercise together!
  • We are tasked with writing a function, naturalNumbers, which takes a positive integer n, and returns a list [1,2,...,n].
  • Try it on your own first!
def naturalNumbers(n):
    ls = [0] * n
    for i in range(n):
        ls[i] = i + 1
    return ls

print(naturalNumbers(11))

Visualize

Negative Indices

  • Unlike many other languages Python allows you to use negative indices
  • By using a negative index you are selecting and index starting at the end of the list
  • For example:
ls = [1,2,3,4]
print(ls[-1]) # Last element
print(ls[-2]) # Second to last element

Visualize

Negative Indices

  • How does this actually work?
  • When the index is negative the following translation is used:
    • <<listName>>[-i] is really just <<listName>>[len(<<listName>>)-i]
  • For example:
ls = [1,2,3,4,5]
print(ls[-1], "is the same as", ls[len(ls)-1])
print(ls[-2], "is the same as", ls[len(ls)-2])

Visualize

More Useful Functions: max

  • The max function can also be used on lists to find the maximum element within that list
  • For example:
ls = [1,2,3,1,3,4,1,5,1,2,3]
print(max(ls))

Visualize

More Useful Functions: sum

  • The sum functions is used to find the sum of a list
  • For example:
ls = [1,2,3]
print(sum(ls))

Visualize

Looping Over Lists

  • Just like strings you can loop over a list directly using a for loop
  • For example:
ls = [1,2,3]
for x in ls:
    print(x)

Visualize

Coding Exercise: for in

  • Let’s do this coding exercise together
  • First try it on your own!
def prod(ls):
    res = 1
    for x in ls:
        res *= x
    return res

print(prod([1,2,3,4]))

Visualize