Lists and indexing

A list stores many values in a single structure.

  • Doing calculations with a hundred variables called pressure_001, pressure_002, etc., would be at least as slow as doing them by hand.

  • Use a list to store many values together.

    • Contained within square brackets [...].

    • Values separated by commas ,.

  • Use len to find out how many values are in a list.

pressures = [0.273, 0.275, 0.277, 0.275, 0.276]
print('pressures:', pressures)
print('length:', len(pressures))
pressures: [0.273, 0.275, 0.277, 0.275, 0.276]
length: 5

Use an item’s index to fetch it from a list.

  • Just like strings.

print('zeroth item of pressures:', pressures[0])
print('fourth item of pressures:', pressures[4])

Lists’ values can be replaced by assigning to them.

  • Use an index expression on the left of assignment to replace a value.

pressures[0] = 0.265
print('pressures is now:', pressures)

Appending items to a list lengthens it.

  • Use list_name.append to add items to the end of a list.

primes = [2, 3, 5]
print('primes is initially:', primes)
print('primes has become:', primes)
  • append is a method of lists.

    • Like a function, but tied to a particular object.

  • Use object_name.method_name to call methods.

    • Deliberately resembles the way we refer to things in a library.

  • We will meet other methods of lists as we go along.

    • Use help(list) for a preview.

  • extend is similar to append, but it allows you to combine two lists. For example:

teen_primes = [11, 13, 17, 19]
middle_aged_primes = [37, 41, 43, 47]
print('primes is currently:', primes)
print('primes has now become:', primes)
print('primes has finally become:', primes)

Note that while extend maintains the “flat” structure of the list, appending a list to a list makes the result two-dimensional - the last element in primes is a list, not an integer.

Use del to remove items from a list entirely.

  • We use del list_name[index] to remove an element from a list (in the example, 9 is not a prime number) and thus shorten it.

  • del is not a function or a method, but a statement in the language.

primes = [2, 3, 5, 7, 9]
print('primes before removing last item:', primes)
del primes[4]
print('primes after removing last item:', primes)

The empty list contains no values.

  • Use [] on its own to represent a list that doesn’t contain any values.

    • “The zero of lists.”

  • Helpful as a starting point for collecting values (which we will see in the [next episode](/12-for-loops/)).

Lists may contain values of different types.

  • A single list may contain numbers, strings, and anything else.

goals = [1, 'Create lists.', 2, 'Extract items from lists.', 3, 'Modify lists.']

Character strings can be indexed like lists.

  • Get single characters from a character string using indexes in square brackets.

element = 'carbon'
print('zeroth character:', element[0])
print('third character:', element[3])

Character strings are immutable.

  • Cannot change the characters in a string after it has been created.

    • Immutable: can’t be changed after creation.

    • In contrast, lists are mutable: they can be modified in place.

  • Python considers the string to be a single value with parts, not a collection of values.

element[0] = 'C'
  • Lists and character strings are both collections.

Indexing beyond the end of the collection is an error.

  • Python reports an IndexError if we attempt to access a value that doesn’t exist.

    • This is a kind of runtime error

    • Cannot be detected as the code is parsed because the index might be calculated based on data.

print('99th element of element is:', element[99])
values = []
print('first time:', values)
values = values[____]
print('second time:', values)

Exercise: How Large is a Slice

If ‘low’ and ‘high’ are both non-negative integers, how long is the list values[low:high]?

Exercise: From Strings to Lists and Back

Given this:

print('string to list:', list('tin'))
print('list to string:', ''.join(['g', 'o', 'l', 'd']))
  1. What does list('some string') do?

  2. What does '-'.join(['x', 'y', 'z']) generate?

Exercise: Working With the End

What does the following program print?

element = 'helium'
  1. How does Python interpret a negative index?

  2. If a list or string has N elements, what is the most negative index that can safely be used with it, and what location does that index represent?

  3. If values is a list, what does del values[-1] do?

  4. How can you display all elements but the last one without changing values? (Hint: you will need to combine slicing and negative indexing.)

Exercise: Through a List

What does the following program print?

element = 'fluorine'

Exercise: Slice Bounds

What does the following program print?

element = 'lithium'

Exercise: Sort and Sorted

What do these two programs print? In simple terms, explain the difference between sorted(letters) and letters.sort().

# Program A
letters = list('gold')
result = sorted(letters)
print('letters is', letters, 'and result is', result)
# Program B
letters = list('gold')
result = letters.sort()
print('letters is', letters, 'and result is', result)

Exercise: Copying (or Not)

What do these two programs print? In simple terms, explain the difference between new = old and new = old[:].

# Program A
old = list('gold')
new = old      # simple assignment
new[0] = 'D'
print('new is', new, 'and old is', old)
# Program B
old = list('gold')
new = old[:]   # assigning a slice
new[0] = 'D'
print('new is', new, 'and old is', old)