Read the position before you read the value
An index says where to look. It is not the data at that position. In codes = ["K02", "B15", "M09"], codes[1] is "B15". In the string "B15", index 1 gives "1". Put the two steps together and codes[1][1] gives "1". When you read unfamiliar code, identify whether each pair of brackets selects a record, a field or a character. Draw the list before doing arithmetic on its indices.
| Index | 0 | 1 | 2 |
|---|---|---|---|
| Code | K02 | B15 | M09 |
Character in B15 | B | 1 | 5 |
A slice stops before the second number. "B15"[1:3] gives "15". "B15"[:1] gives "B". A slice shorter than you expect can still be legal, while a single index outside the string or list causes IndexError. If a task guarantees three-character codes, state that assumption in your plan. If the data can be incomplete, check the length before indexing a required character.
Trace normalisation and filtering
raw = [" bx2 ", "BX2", "a", "m07"]
kept = []
for item in raw:
code = item.strip().upper()
if len(code) == 3 and code not in kept:
kept.append(code)
print(",".join(kept))
The first item is cleaned to BX2 and kept. The second also becomes BX2, so the membership check rejects the duplicate. The third becomes A and fails the length check. The final item becomes M07 and is appended after BX2. The result is BX2,M07. The list keeps the order in which accepted items were found. Sorting the list would solve a different problem. Notice how join builds one string from the items with the separator between them, so there is no comma after the final code.
strip() and upper() return a result. kept.append(code) modifies the existing list. Do not write kept = kept.append(code). The method changes the list but returns None. Also choose between looping over values and looping over positions. for code in kept is enough if you only need the values. for index in range(len(kept)) lets you use the position as well, for example when reporting where the first match occurred.
Store and inspect records
A list such as [["BX2", 6], ["M07", 9]] contains two rows. records[1][0] is the code "M07" and records[1][1] is its score 9. If you are reading a file, split a line into fields first, check that the row has the expected number of fields, and convert only the numeric field. A code like "007" should stay as text if the zeros identify the record.
Practise a Paper 2 change request
Write a short program for a list of team codes. Read a count, then read each code. Clean it with strip() and upper(). Keep a record of the code and its original one-based input position, accepting only three-character codes. Print the position of the first M07, or -1 when it is absent. Plan tests for a duplicate, a short code before the match, and no match. In the browser editor supply the count and each code on its own line. The lesson’s existing case checker tests a different output contract, so use Run with my input for this practice.
Check whether two names refer to one list
second = first makes both names refer to the same list. If you append through second, the change is visible through first too. Use first.copy() when you need a separate shallow copy of a list of codes.