1/5
## Arrays / Lists We've learned about different reference types, let's add them to our smart contract. We'll start with adding a minimal list of favorite numbers. We'll create a new variable called `list_of_numbers`. ```python list_of_numbers: public(uint256[5]) = [0, 0, 0, 0, 0] ``` We've now created a public array called `list_of_numbers` that is five elements long. We can read from this array by interacting with the contract via the terminal, for example: ```bash list_of_numbers ``` To update this array, we'll create a new function called `add_number`. ```python @external def add_number(favorite_number: uint256): self.list_of_numbers[self.index] = favorite_number self.index = self.index + 1 ``` This function takes a `favorite_number` as input and updates the `list_of_numbers` at the current index. To ensure that our `list_of_numbers` doesn't exceed the length of five, we'll create a new variable `index` at the top of our contract: ```python index: public(uint256) = 0 ``` We've set this index to zero in our constructor (`init`) function. We can then use this `index` variable to keep track of the next available element in the array. **Video Tag:** Show this process in the deployed contract in the terminal, make sure to show it with the updated array. We've now found a way to incrementally add numbers to this array. If we try to exceed the length of the array, the transaction will revert.
A practical Vyper tutorial on working with arrays and lists in your smart contracts. The lesson demonstrates how to create an array of a specific size and update its elements using external functions.
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