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.
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.
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 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.
| Operation | Python | C# (List) |
|---|---|---|
| Length | len(lst) | lst.Count (List) or arr.Length (array) |
| Add to end | lst.append(x) | lst.Add(x) |
| Remove item | lst.remove(x) | lst.Remove(x) |
| Remove by index | del lst[i] | lst.RemoveAt(i) |
| Insert at position | lst.insert(i, x) | lst.Insert(i, x) |
| Sort ascending | lst.sort() | lst.Sort() |
| Check membership | x in lst | lst.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)
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.
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(); }
Three Quick Challenges
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]);Add a new score to the end of the list:
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]?
2. A list has 6 elements. What is the index of the last element?
3. What does scores.append(95) do in Python?
4. grid = [[1,2],[3,4],[5,6]]. What is grid[2][0]?
5. What is printed: nums=[3,1,4,1,5]; nums.sort(); print(nums[0])?
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.
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.Practice what you have learned
Three levelled worksheets. Download, print and complete offline.