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

12. HashMap

Vyper's mapping type for key/value storage.

`HashMap[KeyType, ValueType]` is Vyper's equivalent of Solidity's `mapping`. It supports arbitrary key types (including addresses and other hashables) and lazily-initialized default values.

balances: public(HashMap[address, uint256])
allowances: public(HashMap[address, HashMap[address, uint256]])

@external
def deposit():
    self.balances[msg.sender] += msg.value

Like Solidity mappings, a `HashMap` has no length and cannot be iterated — you must track any list of keys separately if you need to enumerate them.

Check your understanding

  1. 1. What is the Vyper equivalent of Solidity's `mapping`?

  2. 2. Can you iterate over all keys in a HashMap?

  3. 3. What is a nested allowance mapping declared as?