Lists

Overview

Teaching: 10 min
Exercises: 10 min
Questions
  • How can I store multiple values?

Objectives
  • Explain why programs need collections of values.

  • Write programs that create flat lists, index them, slice them, and modify them through assignment and method calls.

A list stores many values in a single structure.

subjects = ['women', 'history', 'social conditions']
print('subjects:', subjects)
print('length:', len(subjects))
subjects: ['women', 'history', 'social conditions']
length: 3

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

print('first item of subjects:', subjects[0])
print('third item of subjects:', subjects[2])
first item of subjects: women
third item of subjects: social conditions

Lists’ values can be replaced by assigning to them.

subjects[0] = 'youth'
print('subjects is now:', subjects)
subjects is now: ['youth', 'history', 'social conditions']

Appending items to a list lengthens it.

formats = ["image", "pdf", "web page"]
print('formats is initially', formats)
formats.append("archived web site")
formats.append("drawing")
print('formats has become:', formats)
formats is initially ['image', 'pdf', 'web page']
formats has become: ['image', 'pdf', 'web page', 'archived web site', 'drawing']
mime_types = ["image/gif", "image/jpg"]
other_mime_types = ["image/tif", "image/jpeg"]
print('mime_types is currently:', mime_types)
mime_types.extend(other_mime_types)
print('mime_types has now become:', mime_types)
formats.append(mime_types)
print('formats has finally become:', formats)
mime_types is currently: ['image/gif', 'image/jpg']
mime_types has now become: ['image/gif', 'image/jpg', 'image/tif', 'image/jpeg']
formats has finally become: ['image', 'pdf', 'web page', 'archived web site', 'drawing', ['image/gif', 'image/jpg', 'image/tif', 'image/jpeg']]

Note that while extend maintains the “flat” structure of the list, appending a list to a list makes the result two-dimensional.

Use del to remove items from a list entirely.

pages = [2, 3, 12, 17, 95]
print('pages before removing last item:', pages)
del pages[4]
print('pages after removing last item:', pages)
pages before removing last item: [2, 3, 12, 17, 95]
pages after removing last item: [2, 3, 12, 17]

The empty list contains no values.

Lists may contain values of different types.

tags = [1865, "Tennessee, "railroads"]

Character strings can be indexed like lists.

medium = 'photograph'
print('zeroth character:', medium[0])
print('third character:', medium[3])
zeroth character: p
third character: t

Character strings are immutable.

medium[0] = 'C'
TypeError: 'str' object does not support item assignment

Indexing beyond the end of the collection is an error.

print('99th letter of mediume is:', element[99])
IndexError: string index out of range

String methods

You can use the method split() on a string to split it up on a separator character. Default is space.

title = "Insurance maps of the city of New York"
title_words = title.split()
print(title_words)
['Insurance', 'maps', 'of', 'the', 'city', 'of', 'New', 'York']

Split can take a string to specify another character to split on.

year_info = "2019/2020"
years_list = year_info.split("/")
print(years_list)
['2019', '2020']

How would get just the first year in that list and assign it to a new variable?

start_year = years_list[0]
print(start_year)
2019

Exercise: Split on something else

Given the subject heading below, split apart the subdivisions and assign the results to a list called “subheadings”.

subject = "Railroads--United States--Maps"
subheadings = subject.split("--")
form = subheadings[-1]

Bonus to join them together again

new_subject = '--'.join(subheadings)
Railroads--United States--Maps

Key Points

  • A list stores many values in a single structure.

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

  • Lists’ values can be replaced by assigning to them.

  • Appending items to a list lengthens it.

  • Use del to remove items from a list entirely.

  • The empty list contains no values.

  • Lists may contain values of different types.

  • Character strings can be indexed like lists.

  • Character strings are immutable.

  • Indexing beyond the end of the collection is an error.