Module lessons (1/4)
Lists
Creating, indexing and slicing, adding and removing items, and sorting the sequence you use most.
What you'll learn in this lesson
- Create lists, read elements by positive/negative index and cut out sub-sequences with
[start:stop:step]slicing. - Distinguish in-place methods (
append,extend,sort,reversethat returnNone) from pure functions (sorted,reversed). - Prevent aliasing bugs by explicitly copying mutable lists with
.copy()or[:].
Overview
The list (list) is an ordered and mutable sequence: it can grow, shrink or change contents in memory while maintaining the same object identity. Precisely this mutability makes it very powerful but requires attention when you share references between multiple variables.
When you run alias = nums, Python doesn't duplicate the data: it simply binds a second name on the stack to the same block on the heap. Each alias.append(x) also becomes immediately visible through nums. Use .copy() whenever you want to isolate changes.
Sections covered in this lesson
- 1The mental model: dynamic array of pointers in the heap
- 2Safe slicing and in-place mutation
- 3Sorting (
list.sortvssorted) and independent copies - 4Quick check
- 5Test yourself
Unlock "Lists" and the full Python Course curriculum
Creating, indexing and slicing, adding and removing items, and sorting the sequence you use most.
Unlock all 5 hands-on exercises in this lesson plus every advanced WebAssembly module with automated checks.
- Instant access to all advanced modules in Python Course plus 17 more courses
- Hands-on browser exercises with automated test suites and solutions
- 14-day money-back guarantee processed via Paddle.com
14-day guarantee · Cancel anytimeModule 1, all 18 interactive games, and cheatsheets are always free.