All courses › Programming with Python › Lists

Lists

A list [1, 2, 3] is an ordered, mutable collection of values. You can add to it with append, get its length with len, and slice it just like strings. A tuple (1, 2) is similar but cannot be changed after it is created.

len(x)\text{len}(x)number of elements in the list
x.append(v)x.\text{append}(v)adds vv to the end

Symbols

xxa list
vvthe value being added

Example

x = [1, 2, 3]

x.append(4) makes x [1, 2, 3, 4], and len(x) becomes 4.

Lists are passed to functions by reference: if the function changes the list, you see the change outside too.
Practise python basics for free →

← Strings and indexing · f-strings and formatting →

Part of Programming with Python: Python basics.