Library reference

Number of items

Count the items in a list. Repeated values each count as one item.

len

Simple example

Start4 4 7
Result3
lesson.mojo
def main():
    var values: List[Int] = [4, 4, 7]
    print(len(values))
Output
3
Advertisements

Everyday examples

A delivery list has parcel numbers 41, 17 and 63. How many parcels are due?

Start41 17 63
Result3
lesson.mojo
def main():
    var parcels: List[Int] = [41, 17, 63]
    print(len(parcels))
Output
3

The passenger list contains ticket numbers 8 and 11. How many passengers are listed?

Start8 11
Result2
lesson.mojo
def main():
    var tickets: List[Int] = [8, 11]
    print(len(tickets))
Output
2

Four quiz scores are 2, 2, 5 and 3. How many attempts were recorded, including repeats?

Start2 2 5 3
Result4
lesson.mojo
def main():
    var scores: List[Int] = [2, 2, 5, 3]
    print(len(scores))
Output
4

An empty list has zero items. Its length says how many items it holds.

Run the example
Terminal
mojo lesson.mojo
Official documentation