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.
numbers = [5, 10, -3, 6, 25, 389]
mixed = [-10, 4.5, 100, -3.75, 'hi', 'bye']
empty = []
print(numbers)
print(mixed)
print(empty)[5, 10, -3, 6, 25, 389] [-10, 4.5, 100, -3.75, 'hi', 'bye'] []
[5, 10, -3, 6, 25, 389]Six values, all integers. Neat and tidy — and the usual case.
[-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.
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)3 10 30 [20, 30] [2, 3, 4, 7.5, -3, 'hello'] [1, 2, 3, 1, 2, 3, 1, 2, 3] True True
[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.
numbers = [10, 20, 30]
numbers[0] = 99 # a list allows it
print(numbers)
word = 'hello'
word[0] = 'H' # a string refuses[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 assignmentSame 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.
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.
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)3 [1, 2, 3] 3 1 6 True
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
6Recap
len(), indexing, slicing, + and * all work. And unlike a string, a list can be changed.Which one is a list?
What is [10, 20, 30, 40][1:3]?
Can a list hold a number and a string at the same time?