Skip to content

Latest commit

 

History

History
39 lines (22 loc) · 1.41 KB

mappings.md

File metadata and controls

39 lines (22 loc) · 1.41 KB

Chapter 5: Mappings

In the previous chapter we looked at structs. Mappings are another way of storing organized data in Vyper.

Mappings are hash tables that are virtually initialized such that every possible key exists and is mapped to a default value.

Defining a mapping looks like this:

# a mapping to store roll number and student names
exampleMapping1: HashMap[uint256, String[64]]

# a mapping to store usernames and number of their followers
exampleMapping2: HashMap[String[32], uint256]

A mapping is essentially a key-value store for storing and looking up data. In the first mapping named exampleMapping1, the key is an uint256 and the value is a String[64], and in the second mapping named exampleMapping2 the key is a String[32] and the value is a uint256.

Put it to the test

To store our Pokemons, we will need a mapping which maps a serial number to the Pokemon.

  1. Create a mapping named pokemonList with a uint256 type key and Pokemon type value.

** Template **

embedded-code

** Solution **

embedded-code-final

** Previous Chapter Solution **

embedded-code-previous