Lists
Last updated on 2024-05-15 | Edit this page
Estimated time: 35 minutes
Overview
Questions
- How can I store multiple items in a Python variable?
Objectives
- Create collections to work with in Python using lists.
- Write Python code to index, slice, and modify lists through assignment and method calls.
A list stores many values in a single structure.
The most popular kind of data collection in Python is the list. Lists have two primary important characteristics:
- They are mutable, i.e., they can be changed after they are created.
- They are heterogeneous, i.e., they can store values of many different types.
To create a new list, you can just put some values in square brackets with commas in between. Let’s create a short list of some library metadata standards.
OUTPUT
['marc', 'frbr', 'mets', 'mods']
We can use len()
to find out how many values are in a
list.
OUTPUT
4
Use an item’s index to fetch it from a list.
In the same way we used index numbers for strings, we can reference elements and slices in a list.
OUTPUT
First item: marc
The first three items: ['marc', 'frbr', 'mets']
Reassign list values with their index.
Use an index value along with your list variable to replace a value from the list.
OUTPUT
List was: ['marc', 'frbr', 'mets', 'mods']
List is now: ['bibframe', 'frbr', 'mets', 'mods']
Character strings are immutable.
Unlike lists, we cannot change the characters in a string using its index value. In other words strings are immutable (cannot be changed in-place after creation), while 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.
ERROR
TypeError: 'str' object does not support item assignment
Lists may contain values of different types.
A single list may contain numbers, strings, and anything else (including other lists!). If you’re dealing with a list within a list you can continue to use the square bracket notation to reference specific items.
OUTPUT
First item in sublist: 10
Appending items to a list lengthens it.
Use list_name.append
to add items to the end of a list.
In Python, we would call .append()
a method of the
list object. You can use the syntax of object.method()
to
call methods.
OUTPUT
list was: ['bibframe', 'frbr', 'mets', 'mods']
list is now: ['bibframe', 'frbr', 'mets', 'mods', 'oai-pmh']
Use del
to remove items from a list entirely.
del list_name[index]
removes an item from a list and
shortens the list. Unlike .append()
, del
is
not a method, but a “statement” in Python. In the example below,
del
performs an “in-place” operation on a list of prime
numbers. This means that the primes
variable will be
reassigned when you use the del
statement, without needing
to use an assignment operator (e.g., primes = ...
) .
PYTHON
primes = [2, 3, 5, 7, 11]
print(f'primes before: {primes}')
del primes[4]
print(f'primes after: {primes}')
OUTPUT
primes before: [2, 3, 5, 7, 11]
primes after: [2, 3, 5, 7]
Fill in the Blanks
Fill in the blanks so that the program below produces the output
shown. In the first line we create a blank list by assigning
values = []
.
PYTHON
values = []
values.____(1)
values.____(3)
values.____(5)
print(f'first time: {values})
values = values[____]
print(f'second time: {values})
OUTPUT
first time: [1, 3, 5]
second time: [3, 5]
OUTPUT
databases
- A negative index begins at the final element.
- It removes the final element of the list.
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. - 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.