Python for Interviews

Lists, Tuples & Comprehensions

Sequences: indexing, slicing, mutation, unpacking and comprehensions.

Easy, about 60 minutes to practise.

1Understand

The idea

Lists are dynamic arrays: indexing and append are O(1), but insert(0, x), pop(0), x in lst and slicing are O(n). Comprehensions build a new list in one readable line. Watch out for [[0] * m] * n, which repeats the *same* inner list n times, so changing one row changes them all.

Intuition

A list is a dynamic array: a[i] and append are O(1) amortised, but anything that shifts elements (insert(0, x), pop(0)) or scans (x in a) is O(n). Slicing copies, so b = a[:] is a shallow copy; b = a is only another name for the same list.

Comprehensions build a new list in one readable expression. For 2-D grids, each row must be a new list: [[0] * m for _ in range(n)]. The trap [[0] * m] * n repeats the *same* inner list, so editing one row edits them all.

Tuples are immutable sequences, useful as dict keys and for unpacking (i, x = ...).

What you will be able to do

  • Index, slice and mutate lists
  • Use tuples and unpacking
  • Write comprehensions, including 2-D grids

2See it

Walkthrough

Append is cheap; insert(0) is not

nums = [1, 2], append at the end vs shifting everything right.

1 / 4

1
2
01

Start with a length-2 list. Indexing and append are O(1).

nums = [1, 2]

Prefer append / pop() from the end. Membership on a list is O(n); use a set when you only care about "have I seen this?". Copying with a slice is O(n). Comprehensions are O(n) time and space for the new list.

Read the walkthrough as text

Build a blank grid safely. You need a 3×4 matrix of zeros.

Wrong:

In code:

  • grid = [[0] * 4] * 3
  • grid[0][0] = 1 # every row[0] becomes 1

Right:

In code:

  • grid = [[0] * 4 for _ in range(3)]
  • grid[0][0] = 1 # only row 0 changes
  1. Outer comprehension runs three times, each time creating a fresh [0, 0, 0, 0].
  2. Mutating grid[0][0] touches only that row's list.
  3. Confirm with a second cell: grid[1][0] is still 0.

3Recognise it

Kinds of problems (5)

Same pattern, different surfaces. Each row is a problem shape you should recognise, not only the first walkthrough example.

  • Indexing and negative indices

    Example. Last element, second-to-last, or wrap from the end.

    How. a[-1] is last; a[-k] is k from the end; stay inside [-len, len) except explicit bounds checks.

  • Slicing and copying

    Example. Copy a sublist or clone a slice without sharing tail mutations.

    How. a[start:stop] is a shallow copy of the slice; [:] copies the whole list.

  • enumerate and zip

    Example. Pair names with scores, or walk two lists in lockstep.

    How. for i, x in enumerate(a) when you need the index; zip(a, b) stops at the shorter length.

  • List comprehensions with filters

    Example. Squares of even numbers in one line.

    How. [f(x) for x in a if cond(x)] keeps the loop shape but returns a new list.

  • Building 2-D grids

    Example. Allocate an m×n board initialized to 0.

    How. [[0] * n for _ in range(m)], each row is a fresh list, not [[0]*n]*m.

Common mistakes

Failure modes the tutor watches for. Open the Common mistake tab in the walkthrough to watch one fail live.

  • Mutating a list while iterating

    Why it happens. Removing items inside the same for-loop feels direct.

    How it fails. for x in a: if x == 0: a.remove(x) skips the element after each removal and leaves stray zeros.

    Fix. Iterate a copy, or build a new list, or walk indices backward when deleting in place.

  • Building grids with [[x]*n]*m (aliased rows)

    Why it happens. [[0]*n]*m is shorter than a comprehension and looks equivalent.

    How it fails. grid = [[0]*3]*3; grid[0][0] = 1 makes every row show 1 because all rows are the same list.

    Fix. [[0]*n for _ in range(m)] creates m distinct row lists.

  • Assigning a list instead of copying it

    Why it happens. b = a feels like “make a copy” when you only want to rename.

    How it fails. b = a; b.sort() also sorts a because both names point at one list.

    Fix. Use b = a[:] or list(a) (or copy.copy for shallow) when mutations must not affect the original.

4Check yourself

Concept check

A short quiz on this lesson before you practise. Optional, and it does not gate coding.

Ready to practise?

Mastery comes from solving. The lesson is optional context.

All lessons