Skip to content
ComputerCS
Start learning

Computational Structures · Lesson 4.1.1

Lists

Create lists, read and slice them by index, and grow or shrink them with append, insert and remove.

01

Learning objectives

  • Create a list and read or change any element using positive and negative indexes.
  • Extract a sub-list with slicing and predict exactly which elements a slice returns.
  • Grow a list with append() and insert(), and shrink it with remove() and pop().
  • Compute len(), sum(), min() and max() over a list of numbers.
  • Trace a loop that builds a list step by step and state the final list.
02

Lesson notes

A single variable holds one value; real data rarely comes singly. A class test produces thirty marks, a shopping trip produces a dozen prices, and a cricket innings produces a scorecard of numbers. A list stores many values in one named container, kept in order: marks = [88, 74, 91]. The order matters, because marks[0] is 88 and marks[1] is 74, and the list grows or shrinks while your program runs. Lists are the first data structure every Python programmer meets, and the one the board exam tests most heavily.

Every element lives at a numbered position called an index, and Python counts from zero, not one. In marks = [88, 74, 91, 66, 79], marks[0] is 88 and marks[4] is 79; asking for marks[5] raises an IndexError. Negative indexes count from the end, so marks[-1] is the last element and marks[-2] the second last — a shortcut examiners expect you to know. Because lists are mutable, you can also write marks[1] = 80 and replace a value in place, something strings never allow.

A slice copies out a section: marks[1:4] gives [74, 91, 66] — it starts at index 1 and stops before index 4, so the element at the stop position is never included. This stop-excluded rule confuses everyone at first, but it hides a gift: the length of marks[1:4] is simply 4 − 1 = 3. marks[:3] means from the beginning, marks[3:] means to the end, and marks[:] copies the whole list. Slices never raise errors for out-of-range stops — they just stop early.

Lists come with methods that change them. append(x) adds x at the end — the everyday way to build a list inside a loop. insert(i, x) squeezes x in at position i, shifting everything to the right. remove(x) deletes the first element equal to x (by value, not by position), and pop() removes and returns the last element so you can consume a list item by item. All four work in place: the list object itself changes, and no assignment is needed.

Four built-ins summarise any numeric list instantly: len() counts elements, sum() totals them, and min() and max() pick the extremes — so the class average is sum(marks) / len(marks). Board exam tip: a favourite short question shows three append, insert and remove calls and asks for the final list. Trace them in order on paper, one line per step, and re-check the count with len() after each line — most lost marks come from miscounting after an insert.

A list represented as indexed cells, beginning at index zero.
List indexing starts at zero
03

Worked code examples

Indexes, negative indexes and the big four built-ins

Indexes count from 0, and the slice [1:4] stops before index 4, so only three elements come out. The five marks add up to 398, with 66 the smallest and 91 the largest.

marks = [88, 74, 91, 66, 79]
print(marks[0], marks[-1])
print(marks[1:4])
print(len(marks), sum(marks), min(marks), max(marks))
Output
88 79
[74, 91, 66]
5 398 66 91

Growing and shrinking a list in place

append adds at the end, insert(0, ...) pushes every element right to make room at the front, and remove deletes the first match by value.

queue = ["Ali", "Sara"]
queue.append("Hassan")
queue.insert(0, "Fatima")
queue.remove("Sara")
print(queue)
Output
['Fatima', 'Ali', 'Hassan']

Building a list inside a loop

The empty list grows by one square per loop pass; the total 55 spread over 5 elements gives the mean 11.0.

squares = []
for n in range(1, 6):
    squares.append(n * n)
print(squares)
print(sum(squares) / len(squares))
Output
[1, 4, 9, 16, 25]
11.0
04

Key terminology

element

One value stored inside a list, in a definite position.

index

The position of an element, counted from 0; negative indexes count from the end.

slice

A copied section of a list, written list[start:stop]; the stop index is excluded.

mutable

Able to be changed in place after creation — lists are mutable, strings are not.

method

A function attached to a value and called with a dot, like marks.append(90).

05

Common misconceptions

Common mistake

marks[1] is the first element of the list.

Correct understanding

Indexing starts at 0, so marks[0] is first and marks[1] is second — a favourite examiner trap.

Common mistake

marks[1:4] returns four elements, up to and including index 4.

Correct understanding

Slices stop before the stop index, so [1:4] returns indexes 1, 2 and 3 — exactly three elements.

Common mistake

remove(3) deletes the element at position 3.

Correct understanding

remove() matches by value — it deletes the first element equal to 3. Position-based deletion is pop(3).

06

Check your understanding

1. marks = [88, 74, 91, 66, 79]. What does print(marks[1:4]) show?

2. q = ['a', 'b']; then q.append('c'); q.insert(1, 'x'); q.remove('b'). What is q now?

3. Which expression correctly computes the average of the list marks?

NERDLYY · FEEDBACK

Help shape Nerdlyy

Your feedback goes directly to the team. We read every submission.

No reply needed — you can send feedback without sharing contact details.