typestar

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

  1. append adds to the end, and insert places a value at an index.
  2. pop removes the last element and hands it back.
  3. len reports what is left, and a for loop walks it.

Keywords and builtins used here

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.

Type this snippet

Step 2 of 6 in Collections, step 13 of 26 in Language basics.

← Previous Next →