Vyper theorytheory 0/50 · 0%
Data structures · easy

14. Dynamic arrays with DynArray

Variable-length arrays bounded by a maximum capacity.

`DynArray[T, MAX_LEN]` gives you a growable array, but it is still capped at a compile-time maximum length, so worst-case gas remains bounded. Use `.append()` and `.pop()` to grow and shrink it.

history: public(DynArray[uint256, 100])

@external
def record(value: uint256):
    self.history.append(value)

@external
def undo():
    self.history.pop()

Appending beyond `MAX_LEN` reverts, which is a deliberate safety valve rather than a bug — plan your maximum capacity up front.

Check your understanding

  1. 1. What does the second parameter of DynArray[T, N] mean?

  2. 2. How do you add an element to a DynArray?

  3. 3. What happens if you append beyond the declared max length?