Lists
Last updated on 2026-08-21 | Edit this page
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.
A list is one of the most commonly used data structures in Python. Lists have two 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. Strings and lists can both be indexed and sliced. However, strings are immutable (cannot change in place), while lists are mutable.
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
Using list methods to make adding/removing easier.
Lists are objects, which means they provide methods
that allow us to perform common operations. A method is
called using the syntax object.method().
Lists provide methods for modifying their contents. We call these methods using dot notation. There are many methods associated with a list, but we will cover only a few of them.
Appending items to a list.
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.
OUTPUT
list was: ['bibframe', 'frbr', 'mets', 'mods']
list is now: ['bibframe', 'frbr', 'mets', 'mods', 'oai-pmh']
We can also call list_name.insert(index, value) to add
an item at a given position in the list. The first argument specifies
where to place the new item, and the second argument is the item to
add.
PYTHON
numbers = [1, 2, 3, 4, 6]
print(f'numbers before: {numbers}')
numbers.insert(4, 5)
print(f'numbers after: {numbers}')
OUTPUT
numbers before: [1, 2, 3, 4, 6]
numbers after: [1, 2, 3, 4, 5, 6]
Removing items from a list.
Use list_name.pop() to remove and return the last item
in a list. If we provide an index, .pop() removes and
returns the item at that position. It raises an IndexError if
the list is empty or the index is out of range.
PYTHON
drinks = ['water', 'tea', 'coffee', 'milk']
print(f'drinks before: {drinks}')
drinks.pop()
print(f'drinks after first pop: {drinks}')
val = drinks.pop(0)
print(f'drinks after second pop: {drinks}')
print(val)
OUTPUT
drinks before: ['water', 'tea', 'coffee', 'milk']
drinks after first pop: ['water', 'tea', 'coffee']
drinks after second pop: ['tea', 'coffee']
water
We can also call list_name.remove(value), where value is
the item you want to remove. This will remove the first
occurrence of the item. If there are multiple, then
.remove() will need to be called several times.
PYTHON
animals = ['dog', 'bird', 'shark', 'dog']
print(f'animals before: {animals}')
animals.remove('dog')
print(f'animals after: {animals}')
OUTPUT
animals before: ['dog', 'bird', 'shark', 'dog']
animals after: ['bird', 'shark', 'dog']
Lists: Length and Indexing
- Create a list named
colorscontaining the strings ‘red’, ‘blue’, and ‘green’. - Print the length of the list.
- Print the first color using indexing.
List slicing
- Create a list of numbers defined as [1, 2, 3, 4, 5, 6].
- Print the first three items in the list using slicing.
- Print the last three items using slicing.
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]
Working With the End
OUTPUT
databases
- A negative index begins at the final element.
- It removes the final element of the list.
- It will return the final element of the list.
- 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.
- Use list methods to help add and remove items
- 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.