Python theorytheory 0/50 · 0%
Collections · easy

5. Lists

Ordered, mutable sequences.

Lists are ordered, mutable, and can hold mixed types, though homogeneous lists are more common in practice.

python
txs = ["mint", "transfer", "burn"]
txs.append("swap")
txs[0] = "deploy"
txs.pop()          # removes and returns last item
len(txs)

List comprehensions build new lists concisely:

python
squares = [n * n for n in range(5)]
evens = [n for n in range(10) if n % 2 == 0]

Slicing (`txs[1:3]`) returns a new list, and negative indices count from the end (`txs[-1]` is the last element).

Check your understanding

  1. 1. What does `txs.pop()` do with no argument?

  2. 2. What does `[n*n for n in range(5)]` produce?

  3. 3. What does `txs[-1]` refer to?