How do Python lists and strings store and index sequences of data?
Use Python lists and strings, including indexing, length, looping over items, and basic operations like append and slicing
A focused answer to the O-Level Computing point on Python lists and strings. Creating lists, zero-based indexing, finding length with len(), looping over items, appending, and basic string operations and slicing.
Reviewed by: AI editorial process; not yet individually human-reviewed
Have a quick question? Jump to the Q&A page
Jump to a section
What this dot point is asking
SEAB wants you to use Python lists and strings: creating them, indexing items, finding their length, looping over them, and basic operations such as append and slicing. The central idea is that both a list and a string are ordered sequences whose items have positions (indexes) starting at , so you can reach any item by its index and process every item with a loop.
The answer
Creating a list
A list stores several values in order, written in square brackets:
scores = [40, 75, 60, 90]
names = ["Ravi", "Mei", "Tom"]
A list can hold numbers, strings, or a mix, and it can grow or shrink.
Indexing starts at zero
Each item has an index showing its position, starting at :
scores[0] # 40 (first item)
scores[3] # 90 (fourth item)
The last item of a list of length is at index . A negative index counts from the end: scores[-1] is the last item.
Length with len()
len() gives the number of items in a list or characters in a string:
len(scores) # 4
len("cat") # 3
Looping over items
A for loop visits each item in turn:
for score in scores:
print(score)
Adding to a list
append adds an item to the end:
scores.append(55) # scores is now [40, 75, 60, 90, 55]
Strings are sequences too
A string is a sequence of characters, indexed the same way:
word = "COMPUTER"
word[0] # 'C'
len(word) # 8
Slicing
A slice takes part of a list or string with [start:stop]. The start is included, the stop is excluded:
word[0:4] # 'COMP' (characters 0,1,2,3)
scores[1:3] # [75, 60]
Examples in context
Example 1. A register of names. A teacher stores student names in a list and loops over it to print the register. Adding a new student is a single append, and len(names) gives the class size at any time without counting by hand. Because the names keep their order, the first to register is always names[0], which makes it easy to process them in the order they arrived.
Example 2. Checking a password length. A login program uses len(password) to reject passwords shorter than eight characters, and could slice password[0:3] to show a masked hint such as the first three letters. Treating the password as a sequence of characters makes both the length check and the slice short and clear, and the same indexing ideas apply to any text the program reads.
Try this
Q1. For data = [10, 20, 30], state the value of data[1]. [1 mark]
- Cue. , the second item (indexing starts at ).
Q2. Write a line that adds the number to the end of a list called nums. [2 marks]
- Cue.
nums.append(99).
Q3. State what len("HELLO") returns and why. [2 marks]
- Cue. , because the string has five characters and
lencounts them.
Exam-style practice questions
Practice questions written in the style of SEAB exam questions on this dot point, with worked answer explainers. The year tag is the paper they imitate, not the source.
Original5 marksA list is created with `scores = [40, 75, 60, 90]`. (a) State the value of `scores[0]` and `scores[3]`. (b) Write a for loop that prints each score. (c) Write a line that adds the score to the end of the list.Show worked answer →
(a) Indexing starts at , so scores[0] is the first item, , and scores[3] is the fourth item, .
(b) Loop directly over the list:
for score in scores:
print(score)
(c) Use append to add to the end:
scores.append(55)
After this, scores is [40, 75, 60, 90, 55].
Markers reward scores[0] = and scores[3] = (zero-based), a for loop over the list, and append(55) adding to the end.
Original5 marksA variable holds `word = 'COMPUTER'`. (a) State the value of `word[0]` and `len(word)`. (b) Write a for loop that prints each character on its own line. (c) State what `word[0:4]` produces.Show worked answer →
(a) word[0] is the first character, 'C' (indexing starts at ). len(word) is the number of characters, which is .
(b) Loop over the string:
for letter in word:
print(letter)
(c) word[0:4] is a slice from index up to (but not including) index , giving the first four characters, 'COMP'.
Markers reward word[0] = 'C' and len(word) = , a for loop over the characters, and the slice 'COMP' (stop index excluded).
Related dot points
- Declare and use Python variables, identify the core data types (int, float, str, bool), and convert between them
A focused answer to the O-Level Computing point on Python variables. Assigning variables, the core data types int, float, str and bool, reading input as a string, and converting between types with int(), float() and str().
- Use Python selection with if, elif and else, build conditions with comparison and logical operators, and order branches correctly
A focused answer to the O-Level Computing point on Python selection. Using if, elif and else, comparison operators, combining conditions with and, or and not, and ordering branches so each value falls into the right one.
- Use Python for loops with range and while loops with a condition, including accumulators and avoiding infinite loops
A focused answer to the O-Level Computing point on Python loops. Using for with range for a known count, while with a condition for an unknown count, building accumulators, and avoiding infinite loops.
- Define and call Python functions with parameters and return values, and explain the benefits of using functions
A focused answer to the O-Level Computing point on Python functions. Defining functions with def, passing parameters, returning values, the difference between a parameter and an argument, and why functions make programs reusable.
- Describe and trace linear search and binary search, and explain why binary search needs a sorted list
A focused answer to the O-Level Computing point on searching. How linear search checks each item in turn, how binary search halves a sorted list each step, and why binary search is faster but needs sorted data.