Lesson 7 of 10
Programming: Lesson 7

Lists and Arrays

Storing multiple values under one name. This lesson covers creating lists, zero-based indexing, common methods, traversal patterns, and 2D lists for grids and tables.

55 minutes Python + C# toggle
Language: Saved automatically

Imagine storing the scores of 30 students. With variables alone you would need score1, score2, score3 ... score30 - and a separate variable for each means you cannot loop over them. A list (or array) solves this: one name, 30 values, and a FOR loop can process all 30 in two lines. Every real data structure - a contact book, a shopping cart, a leaderboard - is at its core a list. Understanding lists is the first step towards understanding how programs organise and manipulate data at scale.

Think about it: A program needs to find the average of 100 exam scores entered by the user. Without a list, how would you store them? With a list, write the outline in English. What changes? Why is the list version easier to maintain if the number of students changes?
Terms you need to know
List / Array
An ordered collection of values stored under one name. Python: list. C#: array (fixed size) or List<T> (dynamic).
Index
The position of an element. Starts at 0. scores[0] is the first element, scores[len-1] is the last.
Element
A single item in a list. scores[2] retrieves the element at index 2.
Traversal
Visiting every element in order. Done with a FOR loop: for item in list, or for i in range(len(list)).
Append
Add an element to the end of a list. Python: list.append(x). C# List: list.Add(x).
2D array
A list of lists (a grid). Access with grid[row][col]. Used for tables, game boards and matrices.
Fundamentals

A list groups related values under one identifier. Elements are accessed by their index (position), which starts at 0.

# Creating a list
scores = [85, 72, 91, 64, 78]
names  = ["Alice", "Bob", "Carol"]
mixed  = [1, "hello", True, 3.14]   # Python allows mixed types
empty  = []                           # empty list

# Accessing elements by index (0-based)
print(scores[0])   # 85 (first element)
print(scores[2])   # 91 (third element)
print(scores[-1])  # 78 (last element, negative index)

# Modifying an element
scores[1] = 75     # change second element from 72 to 75

# Length
print(len(scores))  # 5
// C# array - fixed size, declared with type
int[] scores = {85, 72, 91, 64, 78};
string[] names = {"Alice", "Bob", "Carol"};
int[] empty = new int[5];  // array of 5 zeros

// Accessing elements (same 0-based indexing)
Console.WriteLine(scores[0]);   // 85
Console.WriteLine(scores[2]);   // 91
Console.WriteLine(scores[scores.Length - 1]); // 78 (last)

// C# List<T> - dynamic size (more like Python list)
List<int> dynList = new List<int>();
dynList.Add(85);
dynList.Add(72);
Python list vs C# array vs C# List

Python lists are dynamic - you can add and remove elements freely. C# arrays are fixed-size - declared once, the length cannot change. C# List<T> (generic list) is the dynamic equivalent. For GCSE exam purposes, when writing C# you can use either - but be aware of the distinction in real programming.

Working with lists
OperationPythonC# (List)
Lengthlen(lst)lst.Count (List) or arr.Length (array)
Add to endlst.append(x)lst.Add(x)
Remove itemlst.remove(x)lst.Remove(x)
Remove by indexdel lst[i]lst.RemoveAt(i)
Insert at positionlst.insert(i, x)lst.Insert(i, x)
Sort ascendinglst.sort()lst.Sort()
Check membershipx in lstlst.Contains(x)
# Traversal with for-in (recommended for visiting all elements)
scores = [85, 72, 91, 64, 78]
for score in scores:
    print(score)

# Traversal with index (needed when modifying or needing the index)
for i in range(len(scores)):
    scores[i] = scores[i] + 5     # add 5 to every score

# Accumulator pattern with list
total = 0
for score in scores:
    total += score
average = total / len(scores)
print("Average:", average)
Exam angle - the off-by-one error

A list with 5 elements has indices 0, 1, 2, 3, 4. The last valid index is len(list) - 1, NOT len(list). Accessing scores[5] on a 5-element list causes an IndexError. This is the most common list bug in exam programs. In trace questions: always check whether the question uses 0-indexed (programming) or 1-indexed (some pseudocode) notation.

Grids and tables

A 2D array (list of lists) represents a grid. Access elements with grid[row][col] - row first, then column.

# 3x3 grid (list of lists)
grid = [
    [1, 2, 3],   # row 0
    [4, 5, 6],   # row 1
    [7, 8, 9]    # row 2
]
print(grid[1][2])   # 6  (row 1, col 2)
print(grid[0][0])   # 1  (top-left)

# Traverse a 2D grid
for row in grid:
    for item in row:
        print(item, end=" ")
    print()
// C# 2D array
int[,] grid = {
    {1, 2, 3},
    {4, 5, 6},
    {7, 8, 9}
};
Console.WriteLine(grid[1, 2]);   // 6 (row 1, col 2)

// Traverse
for (int r = 0; r < 3; r++) {
    for (int c = 0; c < 3; c++)
        Console.Write(grid[r, c] + " ");
    Console.WriteLine();
}
Array Visualiser
List / Array Explorer
Enter comma-separated values to build a list, then click operations to see the code and result.
Build a list above, then click an operation.

Three Quick Challenges

Predict the output

What does negative indexing print here?

scores = [85, 90, 72, 95]
print(scores[-1])
int[] scores = {85, 90, 72, 95};
Console.WriteLine(scores[scores.Length - 1]);
Fill in the blank

Add a new score to the end of the list:

scores.(99)
scores.(99);
Spot the bug

This code crashes with an IndexError. Why?

names = ["Alice", "Bob", "Carol"]
print(names[3])

Test yourself

1. nums = [10, 20, 30, 40, 50]. What is nums[3]?

Index 3 is the fourth element (indices 0,1,2,3). nums = [10, 20, 30, 40, 50]. Index 0=10, 1=20, 2=30, 3=40, 4=50. So nums[3] = 40.

2. A list has 6 elements. What is the index of the last element?

A list with 6 elements has indices 0, 1, 2, 3, 4, 5. The last index is always len - 1 = 6 - 1 = 5. Accessing index 6 on a 6-element list gives an IndexError.

3. What does scores.append(95) do in Python?

append() adds a new element to the END of the list. The list grows by one element. To add at the start, use insert(0, 95). To replace the last element, use scores[-1] = 95.

4. grid = [[1,2],[3,4],[5,6]]. What is grid[2][0]?

grid[2] is the third row: [5, 6]. grid[2][0] is the first element of that row: 5. Remember: first index = row, second index = column. Both are zero-indexed.

5. What is printed: nums=[3,1,4,1,5]; nums.sort(); print(nums[0])?

sort() sorts the list in-place (modifying the original). [3,1,4,1,5] sorted ascending = [1,1,3,4,5]. nums[0] is now 1. Note: sort() modifies the list; sorted(nums) returns a new sorted list without changing nums.
Challenge question

Write a program that removes all duplicate values from a list, keeping only the first occurrence of each. For example, [3, 1, 4, 1, 5, 9, 2, 6, 5] should become [3, 1, 4, 5, 9, 2, 6]. You cannot use any built-in deduplication functions. Describe your algorithm in English first, then write the code.

Algorithm: Create an empty result list. For each element in the original list, check if it is already in the result list. If not, append it. Skip it if it is already there.

Python:
original = [3, 1, 4, 1, 5, 9, 2, 6, 5]
unique = []
for item in original:
    if item not in unique:
        unique.append(item)
print(unique) # [3, 1, 4, 5, 9, 2, 6]

The in operator checks list membership. This is O(n^2) but correct. A faster version would use a set to track seen values, but this approach demonstrates the core list skills tested at GCSE.
Printable Worksheets

Practice what you have learned

Three levelled worksheets. Download, print and complete offline.

Recall
List Operations Reference
Label list diagrams, trace element access and complete operation tables for append, remove and sort.
Download
Apply
List Processing Programs
Write programs to find averages, search for values, sort data and process 2D grids.
Download
Exam Style
Exam-Style Questions
Trace list operations, predict outputs, identify index errors and write list-based search and sort routines.
Download
Lesson 7 - Programming
Lists and Arrays
Starter activity
Ask students to stand up. Tell them they are "score 1, score 2, score 3..." and write their index on a label. Then ask "What is student 3's score?" - students realise index 3 is the FOURTH person. This physically demonstrates zero-indexing before any code is written.
Lesson objectives
1
Create lists and arrays and access elements by zero-based index.
2
Modify, append, remove and sort list elements.
3
Traverse a list with a for-in loop and with an index loop.
4
Create and traverse 2D lists/arrays using grid[row][col].
5
Identify and explain IndexError (off-by-one) bugs.
Key vocabulary
Index (zero-based)
Position of element starting from 0. Last element at index len - 1.
Traversal
Visiting every element in order. Use for-in for values; for-range for index access.
Discussion questions
Why is zero-based indexing used in most programming languages? What everyday confusion does this cause?
What is the risk of accessing list[-1] in a program? When is it useful and when could it produce unexpected results?
Can you think of three real-world scenarios where data would naturally be stored in a list rather than individual variables?
Exit tickets
Write code to find and print the largest value in scores = [45, 72, 38, 91, 60] without using max(). [4 marks]
grid=[[1,2,3],[4,5,6]]. What is grid[0][2]? What is grid[1][0]? [2 marks]
A student writes: for i in range(len(scores)+1): print(scores[i]). What error occurs and why? [2 marks]
Homework suggestion
Write a program that collects 5 exam scores from the user (with validation: must be 0-100), stores them in a list, then outputs: the highest score, lowest score, average, and how many scores were above the average.