King's College London · FACULTY OF ARTS & HUMANITIES

7AAVDH10 Chap.2 Lists, Indices and Slices

- one subject, every graph, every model, every mark
6 Chapters4-page Bible
Our own words - no uploaded lecturer files
Updated for this semester
Chapter 2 of 6 · 7AAVDH10

Lists, Indices and Slices

Read sequence structure explicitly

Lists reuse Python's indexing and slicing rules while adding mutation. This chapter distinguishes retrieving one item, selecting a range, replacing an existing item, appending one item and combining two lists.

Nested records are traced one bracket at a time so a syntactically valid expression cannot silently select the wrong row or field.

Make mutation visible

A list assignment changes an existing object, whereas a string transformation constructs a new value.

The chapter uses before-and-after states, sampled records and field descriptions to connect this programming difference to the reliability of humanities data work.

In this chapter

What this chapter covers

  • 01

    List syntax

  • 02

    Item mutation

  • 03

    Append method

  • 04

    List concatenation

  • 05

    Nested records

  • 06

    Aliasing checks

Worked example · free

Retrieve a nested record field

Q [6 marks]. Select the title from the second title-year record and explain both indices. The marks shown are a study allocation, not an official University marking scheme.
Nested access.Nested access. The first index crosses the outer records collection; the second index selects a field within the chosen record. [title, year][title, year]recordsrecords[1][0] selects the second title
Nested access. The first index crosses the outer records collection; the second index selects a field within the chosen record.
  • 2Select the outer record. The marks shown are a study allocation, not an official University marking scheme.
  • 2Inspect the intermediate list.
  • 2Select the title field.
The first index selects the second inner list, and the second index selects position zero within that record. Printing the intermediate record proves which dimension each bracket crosses. The marks shown are a study allocation, not an official University marking scheme.
Sia tip — Print the intermediate record before applying the second index.
Glossary

Key terms

List
A list is a mutable ordered sequence of Python values.
Mutation
Mutation changes the contents of an existing object.
Nested List
A nested list is a list containing one or more lists as items.
FAQ

Lists, Indices and Slices FAQ

How is append different from addition?

The append method changes one list by adding its argument as a single new item. List addition creates a combined sequence from two lists, so the choice depends on whether mutation or a new value is intended.

Why did my list become nested?

Appending a list adds that whole list as one item. Use separate append calls or combine two lists when the goal is to add their individual elements at the outer level.

How can I debug two bracket pairs?

Evaluate the leftmost bracket first and print that intermediate value. Confirm that it is the expected inner record, then apply the next index to the documented field position.

When should I replace positional records?

Positional lists become risky when records have many fields, inconsistent lengths or a field order that readers must repeatedly remember. Sample several rows and write the meaning and expected type of every position. If that description is difficult to maintain, move to dictionaries or a dataframe with named columns.

The change does not automatically clean the data, but it makes field selection more explicit and allows validation to refer to a meaningful name instead of an unexplained number.

Study strategy

Exam move

Draw the outer and inner sequence as boxes, then trace each bracket to a specific box. Alternate two questions throughout revision: what value is returned, and what object state changes? Begin with a four-item list and practise positive indices, negative indices and three slices whose intervals touch. Mark the excluded stop before writing an answer. Next compare assignment, append and list addition.

Print the source list before and after each operation, state whether mutation occurred and decide whether that effect matches the intended workflow. Build a nested record set with title and year fields. Evaluate the first bracket alone, inspect the intermediate record, then evaluate the field index. Add one malformed record with reversed fields and one short record with a missing year.

Observe that valid Python syntax can still retrieve conceptually wrong evidence when record structure is inconsistent. Write a structural sentence beside the code, such as: each outer item is a record, position zero is title and position one is year. Use that sentence as an assertion to check sample rows. Explore aliasing by assigning a second variable name to the same list, mutating one item and printing both names.

Explain why a new name did not automatically create a copy. If the project must preserve raw data, identify a strategy that avoids accidental mutation and verify it with a before-and-after comparison. Keep an error ledger divided into position, range, nesting, mutation and aliasing. For every fault, record the earliest intermediate value that differs from expectation.

In exam preparation, trace brackets from left to right without running code. In project preparation, sample first, last, empty and malformed cases before applying an access pattern to the full collection. Close the session by deciding whether positional lists remain readable for the current data. When fields become wide or unstable, explain why dictionaries or a dataframe provide safer named access.

Once a week, rebuild a small collection from scratch with two valid records, one duplicated record and one damaged record. Retrieve the same conceptual field from each valid row and explain why the damaged row cannot safely support that access pattern. Practise a non-mutating combination beside an in-place append, then ask which variables expose the changed state.

Save the expected before-and-after lists in markdown before running code. Finish with a migration exercise: rewrite a nested list as dictionaries with named keys and compare the readability of the two lookups. The goal is not to declare one structure universally better, but to identify when positional compactness stops compensating for the risk of forgotten field order.

A+Everything unlocked
Unlocks this Bible + all 18 of your subjects at King's College London, plus 1,000+ more Bibles across the AskSia library.
Sia - your 7AAVDH10 tutor, unlimited, worked the way the exam marks it
The full 4-page Bible + practice bank with worked solutions
Chrome extension - sync your LMS so Sia knows your deadlines
Bilingual EN / Chinese on every Bible and every Sia answer
$0.99 Trial
30-day money-back · cancel in one tap · how it works