0/5
## Retrieving Data From a Hashmap In the previous lessons, we created a smart contract that allowed us to add people to a list. However, we need to know the index of a person on the list in order to retrieve their data. Hashmaps, also known as mappings, are incredibly powerful because they allow us to retrieve data without knowing the index. We can think of a hashmap like a dictionary, with a string key and a uint256 value. In our smart contract, we are going to create one more storage variable called `name_to_favorite_number`, which will be a hashmap of type `string` and `uint256`. ```python name_to_favorite_number: public(HashMap[string, uint256]) ``` For example, we can add the name `Mariah` and associate her with the number 30. In the `add_person` function, we are going to add the person to the hashmap. ```python @external def add_person(name: String[100], favorite_number: uint256): # Add favorite number to the numbers list self.list_of_numbers[self.index] = favorite_number # Add the person to the person's list new_person: Person = Person(favorite_number = favorite_number, name = name) self.list_of_people[self.index] = new_person # Add the person to the hashmap self.name_to_favorite_number[name] = favorite_number self.index = self.index + 1 ``` Now, we can use the `name_to_favorite_number` function to retrieve Mariah's favorite number, without needing to know her index. ```bash name_to_fav... mariah ``` We can see that Mariah's favorite number is 30. ```bash 0: uint256:30 ``` This is the power of hashmaps, or mappings, in smart contracts. They allow us to quickly retrieve data without knowing the index. As we learn more about smart contract programming, we will understand the trade-offs of each data structure and when to use each one.
A powerful introduction to using Hash Maps (Mapping) in Vyper smart contract programming. This lesson covers the creation of a new storage variable called 'name_to_favorite_number', which is of type Hash Map.
Previous lesson
Previous
Next lesson
Next
Give us feedback
Course Overview
About the course
The basics of blockchain transactions, how to send and receive money on a blockchain network.
How to write Python based smart contracts using Vyper.
How to read and understand Vyper smart contracts.
Vyper data structures, arrays, structs, hash maps.
How to build a smart contract application and deploy on ZKsync with Moccasin.
Smart Contract Auditor
$100,000 - $200,000 (avg. salary)
On-chain Data Analyst
$59,000 - $139,000 (avg. salary)
DeFi Developer
$75,000 - $200,000 (avg. salary)
Smart Contract Engineer
$100,000 - $150,000 (avg. salary)
Web3 developer
$60,000 - $150,000 (avg. salary)
Web3 Developer Relations
$85,000 - $125,000 (avg. salary)
Web3 engineer, educator, and Cyfrin co-founder. Patrick's smart contract development and security courses have helped hundreds of thousands of engineers kickstarting their careers into web3.
Last updated on March 21, 2025
Duration: 2h 08min
Duration: 2h 32min
Duration: 24min
Course Overview
About the course
The basics of blockchain transactions, how to send and receive money on a blockchain network.
How to write Python based smart contracts using Vyper.
How to read and understand Vyper smart contracts.
Vyper data structures, arrays, structs, hash maps.
How to build a smart contract application and deploy on ZKsync with Moccasin.
Smart Contract Auditor
$100,000 - $200,000 (avg. salary)
On-chain Data Analyst
$59,000 - $139,000 (avg. salary)
DeFi Developer
$75,000 - $200,000 (avg. salary)
Smart Contract Engineer
$100,000 - $150,000 (avg. salary)
Web3 developer
$60,000 - $150,000 (avg. salary)
Web3 Developer Relations
$85,000 - $125,000 (avg. salary)
Web3 engineer, educator, and Cyfrin co-founder. Patrick's smart contract development and security courses have helped hundreds of thousands of engineers kickstarting their careers into web3.
Last updated on March 21, 2025
Testimonials
Read what our students have to say about this course.
Chainlink
Chainlink
Gustavo Gonzalez
Solutions Engineer at OpenZeppelin
Francesco Andreoli
Lead Devrel at Metamask
Albert Hu
DeForm Founding Engineer
Radek
Senior Developer Advocate at Ceramic
Boidushya
WalletConnect
Idris
Developer Relations Engineer at Axelar