LambdaLabTM
Computer Science · Class 11 · Data Types
Data TypesSequences⏱️ 9 min read

Lists

A list is a collection of values inside square brackets, separated by commas. It is the most used data type in all of Python, for one reason: unlike everything you have met so far, a list can be changed.

1Making a list

Values, commas, square brackets. That is the whole recipe. The values may all be of one type, or of several — a list does not mind.

making_lists.py
numbers = [5, 10, -3, 6, 25, 389]
mixed = [-10, 4.5, 100, -3.75, 'hi', 'bye']
empty = []

print(numbers)
print(mixed)
print(empty)
Output
[5, 10, -3, 6, 25, 389]
[-10, 4.5, 100, -3.75, 'hi', 'bye']
[]
All one type
[5, 10, -3, 6, 25, 389]

Six values, all integers. Neat and tidy — and the usual case.

A mixture
[-10, 4.5, 'hi']

An integer, a float and a string, living together quite happily. Python allows it.

2It is a sequence — so you already know it

A list belongs to the Sequential family, which means all six common features work on it exactly as they did on a string. Nothing new to learn here — that is the reward for learning them once.

list_recap.py
mixed = [10, 4.5, 'hi']
numbers = [10, 20, 30]

print(len(mixed))
print(numbers[0])
print(numbers[-1])
print([10, 20, 30, 40][1:3])          # a list can be sliced where it stands
print([2, 3, 4] + [7.5, -3, 'hello'])
print([1, 2, 3] * 3)
print(20 in numbers)
print(50 not in numbers)
Output
3
10
30
[20, 30]
[2, 3, 4, 7.5, -3, 'hello']
[1, 2, 3, 1, 2, 3, 1, 2, 3]
True
True
Tip
Slicing a list gives you back a list, not a single value — even when it holds just one item. Indexing gives the item itself. [10, 20, 30][0] is 10, but [10, 20, 30][0:1] is [10].

3The big difference: a list can be changed

Everything you have met so far — numbers, booleans, strings — refuses to be edited. A list does not. Point at a position and assign a new value to it, and the list simply obeys.

changing.py
numbers = [10, 20, 30]
numbers[0] = 99      # a list allows it
print(numbers)

word = 'hello'
word[0] = 'H'        # a string refuses
Output
[99, 20, 30]
Traceback (most recent call last):
  File "changing.py", line 6, in <module>
    word[0] = 'H'        # a string refuses
    ~~~~^^^
TypeError: 'str' object does not support item assignment

Same instruction, twice. The list obeyed — and the print proves it, showing 99 where 10 used to be. The string raised an error instead, and the program stopped there. That one difference is why lists exist, and it has a proper name, which this chapter comes to once tuples have shown you the opposite case.

Key Takeaway
A list is changeable. You can replace an item, and — with the methods in the next lesson — add items, remove them and sort them. A string cannot be changed at all.

4Things Python will do to a list for you

These are functions — the list goes inside the parentheses — and each one hands back a new answer without disturbing the list.

list_functions.py
numbers = [3, 1, 2]

print(len(numbers))
print(sorted(numbers))
print(max(numbers))
print(min(numbers))
print(sum([1, 2, 3]))
print(3 in numbers)
Output
3
[1, 2, 3]
3
1
6
True
Note
Lists also have methods of their own — append(), remove(), sort() and several more — written after the name with a dot, like numbers.append(40). Unlike the functions above, they change the list in place instead of handing back a new answer. That difference is worth a lesson of its own, and it is the next one.

5Try it at the prompt

Python prompt — interactive mode
# Build a list, slice it, join two together.
>>>
try

6Recap

Key Takeaway
A list is values in square brackets, separated by commas. It can hold mixed types. It is a sequence, so len(), indexing, slicing, + and * all work. And unlike a string, a list can be changed.
Quick Check

Which one is a list?

Quick Check

What is [10, 20, 30, 40][1:3]?

Quick Check

Can a list hold a number and a string at the same time?