L2·Control & Collections

📋Lists

Ordered, mutable sequences.

listappendslicesort

§ 1Creation & indexing

[1,2,3] literal. Zero-indexed. Negative indices count from the end.

§ 2Mutation

.append(x), .extend(it), .insert(i,x), .pop(i), .remove(x), del lst[i].

§ 3Slicing

lst[start:stop:step] returns a new list. lst[:] is a shallow copy.

Example

1nums = [3, 1, 4, 1, 5, 9, 2, 6]
2nums.sort()
3print(nums[:3]) # [1, 1, 2]
4print(nums[::-1]) # reversed

Try it live · Python runs in your browser

Loading playground…

Checkpoint quiz

Question 1 / 3
Are lists mutable?