1.2.21 · D4Introduction to Programming (Python)

Exercises — Lists — creation, indexing, slicing, mutability

2,070 words9 min readBack to topic

The number line under a list is the mental picture for everything below:

Figure — Lists — creation, indexing, slicing, mutability

L1 — Recognition

Recall Solution

The list has length , so valid forward indices are .

  • a[0]'c' — offset 0 = first box.
  • a[4]'k' — offset = last box.
  • a[-1] → same as a[n-1] = a[4]'k'.
  • a[-5] → same as a[n-5] = a[0]'c'.

Answer: 'c', 'k', 'k', 'c'.

Recall Solution
  • b = asame object (a second label on the one box).
  • c = a[:] → a full slice builds a new list → separate object.
  • d = a.copy() → also a new list → separate object.

Answer: only b shares a's object; c and d are independent copies.


L2 — Application

Recall Solution
  • a[3:7] → indices 3,4,5,6 (7 excluded) → [3, 4, 5, 6]. Length . ✓
  • a[:4] → start defaults to 0 → indices 0,1,2,3 → [0, 1, 2, 3].
  • a[6:] → stop defaults to → indices 6,7,8,9 → [6, 7, 8, 9].
  • a[3:7:2] → land on 3, then 3+2=5, then 5+2=7 (but 7 is not < 7, stop) → [3, 5]. Length . ✓
Recall Solution

Convert both endpoints with :

  • (start), (stop, excluded).
  • So this is a[2:5] → indices 2,3,4 → [30, 40, 50].
  • Length . ✓

Answer: [30, 40, 50], length 3.


L3 — Analysis

Recall Solution

A negative step walks from the end toward the start.

  • a[::-1] → every box, backward → [9, 8, 7, 6, 5, 4, 3, 2, 1, 0] (a full reverse).
  • a[::-2] → start at last (9), jump back by 2: 9, 7, 5, 3, 1 → [9, 7, 5, 3, 1].
  • a[8:2:-2] → start at index 8, go backward by 2, stop before index 2 (2 excluded): land on 8, 6, 4 (next is 2 which is the stop) → [8, 6, 4].

See the two directions of travel:

Figure — Lists — creation, indexing, slicing, mutability
Recall Solution
  • b = ab is a second label on a's list. c = a[:]c is a new list.
  • b.append(4) edits the shared list → both a and b become [1, 2, 3, 4].
  • c.append(9) edits only c's separate list → c = [1, 2, 3, 9]; a untouched.

Answer: a = [1, 2, 3, 4], b = [1, 2, 3, 4], c = [1, 2, 3, 9].

Two labels, one box vs one label, own box:

Figure — Lists — creation, indexing, slicing, mutability

L4 — Synthesis

Recall Solution
  • a.append(3) mutates a in place → a = [1, 2, 3], and it returns None, so r1 = None.
  • a + [4] builds a brand-new list [1, 2, 3, 4]b = [1, 2, 3, 4]; a stays [1, 2, 3] (the + did not touch it).
  • a = a.append(5) — the .append(5) first mutates a in place to [1, 2, 3, 5], but returns None, and that None is assigned back to a. So a = None (the list still exists in memory but you lost the name for it).

Answer: r1 = None, b = [1, 2, 3, 4], final a = None.

Recall Solution

Use a slice with step -1. A slice always returns a new list, so a is safe:

r = a[::-1]      # [5, 1, 4, 1, 3]  ← new object
# a is still [3, 1, 4, 1, 5]

Contrast with a.reverse(), which mutates a in place and returns None — that would destroy the original order. Slice = copy + reverse in one shot.

Answer: r = [5, 1, 4, 1, 3], and a = [3, 1, 4, 1, 5] (unchanged).


L5 — Mastery

Recall Solution

Use the length formula for step : Why the ceiling? You always get to take the first landing point (start), then each further step that stays below stop. Confirm by listing the landings: 2, 6, 10, 14 (next would be 18, but → stop). Four items. ✓

Answer: 4 items → [2, 6, 10, 14].

Recall Solution

a[1:4] targets the three boxes at indices 1,2,3 (values 1, 2, 3; index 4 excluded). Slice assignment replaces that whole span with the new list — and the replacement need not be the same length. Splice out [1, 2, 3], splice in [10, 20]:

[0, |1, 2, 3|, 4, 5]   →   [0, |10, 20|, 4, 5]

The list shrinks by one (removed 3 items, inserted 2).

Answer: a = [0, 10, 20, 4, 5], new length 5.

Recall Solution

"Every second, from the end, end-to-start" = start at the last box, step by . Omit start and stop so Python uses "last box" and "past the front":

r = a[::-2]     # [8, 6, 4, 2]

A slice returns a new object, so a stays [1,2,3,4,5,6,7,8]. Trace: start 8 (index 7), then index 5 → 6, index 3 → 4, index 1 → 2, next would be index −1 (off the front) → stop.

Answer: r = [8, 6, 4, 2], a unchanged.


Recall One-screen recap
  • Negative index: . Convert coldly, never eyeball.
  • Slice [start:stop:step]: include start, exclude stop, any step sign. Length .
  • Slices return new lists (safe copies); a[:], .copy(), list(a) all copy.
  • append/sort/reverse mutate in place and return None — never assign their result.
  • Contiguous slice assignment can resize the list.

See also: Strings — indexing & slicing (same slice rules on text), Tuples (ordered but immutable), List methods — append, insert, pop, sort, Shallow vs Deep copy, for loops, Mutability & function arguments.