Programming with Python

Storing Values in Lists

Learning Objectives

  • Explain what a list is.
  • Create and index lists of simple values.

Lists are containers that can hold many values. Unlike pandas DataFrames, lists are built into the language (so we don’t have to load a library to use them). We create a list by putting values inside square brackets:

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

We select individual elements from lists by indexing them:

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

and if we loop over a list, the loop variable is assigned elements one at a time:

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']

works, but:

name = 'Bell'
name[0] = 'b'
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-8-220df48aeb2e> in <module>()
      1 name = 'Bell'
----> 2 name[0] = 'b'

TypeError: 'str' object does not support item assignment

does not.

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, 7, 11]
del odds[0]
print 'odds after removing the first element:', odds
odds after removing the first element: [3, 5, 7, 11]
odds.reverse()
print 'odds after reversing:', odds
odds after reversing: [11, 7, 5, 3]

While modifying in place, it is useful to remember that python treats lists in a slightly counterintuitive way.

If we make a list, (attempt to) copy it, and then modify it in place, we can run into 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, when we ‘copy’ it using =, simply adds a new name to refer to the same object. If we want to do is copy a list, we can use the list() command to clone it and create a new and independent list:

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]

This is different from how variables worked in lesson 1, and more similar to how a spreadsheet works.

Turn a string into a list

Convert the string “hello” into a list of letters:

["h", "e", "l", "l", "o"]

Hint: You can create an empty list and progressively append letters:

my_list = []
my_list.append('h') # etc

Then make new lists for words that are similar to “hello” (for example, “jello”), by modifying, deleting, and appending to the list as needed.