Growing and shrinking a list in Mojo
append, insert, and pop cover most list edits.
def main():
var queue: List[Int] = [10, 20]
queue.append(30)
queue.insert(0, 5)
var last = queue.pop()
print("popped", last, "and", len(queue), "remain")
for value in queue:
print(value)
How it works
appendadds to the end, andinsertplaces a value at an index.popremoves the last element and hands it back.lenreports what is left, and aforloop walks it.
Keywords and builtins used here
IntListdefforlenmainprintvar
The run, in numbers
- Lines
- 8
- Characters to type
- 186
- Tokens
- 66
- Three-star pace
- 85 tpm
At the three-star pace of 85 tokens a minute, this run takes about 47 seconds.
Step 2 of 6 in Collections, step 13 of 26 in Language basics.