Skip to main content
Module 3 · Lesson 1 of 49/36 in the course~16 min
Module lessons (1/4)

Lists

Lesson preview5 hands-on exercises readyPython Course · Module 3 — Data structures

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, reverse that return None) 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

  1. 1The mental model: dynamic array of pointers in the heap
  2. 2Safe slicing and in-place mutation
  3. 3Sorting (list.sort vs sorted) and independent copies
  4. 4Quick check
  5. 5Test yourself
Pro Lesson · Module 3 — Data structures

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.