Programming with Python

Storing Multiple Values in Lists

Learning Objectives

  • Create lists and access items in a list.
  • Understand the differences between mutable and immutable objects.
  • Make copies of mutable objects.

In the first lesson we used NumPy arrays to manipulate temperature data. NumPy arrays are not built into Python; we had to import the library numpy in order to be able to use them. These data structures are the most useful for scientific computing because they allow us to easily do math on tabular data or grids. For anyone who’s programmed in Matlab or C before, these are also the most familiar data structure (which is why we introduce them first!).

One of the built-in data structures that you will use extensively in Python is the list. Because they are built in, we don’t need to load a library to use them. A list is exactly what it sounds like – a sequence of things. Lists are ordered, so we can access the items through an integer index (like we did with NumPy arrays), and one list can simultaneously contain numbers, strings, other lists, numpy arrays, etc.

Lists are created by putting values, separated by commas, inside square brackets:

odds = [1, 3, 5, 7]
print 'odds are:', odds
odds are: [1, 3, 5, 7]

Because they are ordered, we can select individual elements from lists by indexing:

print 'first and last:', odds[0], odds[-1]
first and last: 1 7

We can also assign new values to individual elements in a list through indexing:

odds[-1] = 9
print 'odds are now:', odds
odds are now: [1, 3, 5, 9]

There are many ways to change the contents of lists besides assigning new values to individual elements:

odds.append(11)
print 'odds after adding a value:', odds
odds after adding a value: [1, 3, 5, 9, 11]
del odds[0]
print 'odds after removing the first element:', odds
odds after removing the first element: [3, 5, 9, 11]
odds.reverse()
print 'odds after reversing:', odds
odds after reversing: [11, 9, 5, 3]

There is one important difference between lists and strings: we can change the values in a list, but we cannot change the characters in a string. For example:

names = ['Newton', 'Darwing', 'Turing'] # typo in Darwin's name
print 'names is originally:', names
names[1] = 'Darwin' # correct the name
print 'final value of names:', names
names is originally: ['Newton', 'Darwing', 'Turing']
final value of names: ['Newton', 'Darwin', 'Turing']
name = 'Bell'
name[0] = 'b'
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-15-220df48aeb2e> in <module>()
      1 name = 'Bell'
----> 2 name[0] = 'b'

TypeError: 'str' object does not support item assignment

If we make a list, assign its variable name to another name and then modify the list, we can cause all sorts of trouble:

odds = [1, 3, 5, 7]
primes = odds
primes += [2]
print 'primes:', primes
print 'odds:', odds
primes: [1, 3, 5, 7, 2]
odds: [1, 3, 5, 7, 2]

This is because python stores a list in memory and modifies it in place whenever we ask to change its value. Consequently, it will also allow multiple variable names to point to the same object in memory. If we want to actually copy a list (and make a second copy somewhere else in the memory), we can use the list() command:

odds = [1, 3, 5, 7]
primes = list(odds)
primes += [2]
print 'primes:', primes
print 'odds:', odds
primes: [1, 3, 5, 7, 2]
odds: [1, 3, 5, 7]