Collections¶
Lists¶
A list is an ordered, mutable sequence of values.
Generic type annotations¶
Annotate the element type for static checking:
Indexing¶
Zero-based. Negative indices count from the end:
Assignment by index¶
Iteration¶
Membership test¶
Slicing¶
Extract a sub-list using [start:end] or [start:end:step]. Indices are end-exclusive. Negative indices count from the end.
nums = [10, 20, 30, 40, 50]
write(nums[1:3]) # [20, 30]
write(nums[:3]) # [10, 20, 30]
write(nums[2:]) # [30, 40, 50]
write(nums[::2]) # [10, 30, 50] (every other element)
write(nums[::-1]) # [50, 40, 30, 20, 10] (reversed)
Slice indices must be integers. A step of 0 raises a ZeroDivisionFault.
List comprehensions¶
Build a new list from an expression and an optional filter:
squares = [x * x for x in [1, 2, 3, 4, 5]] # [1, 4, 9, 16, 25]
evens = [x for x in range(1, 10) if even(x)] # [2, 4, 6, 8]
upper = [s.uppercase() for s in ["a", "b", "c"]] # ["A", "B", "C"]
List built-ins¶
| Function | Description |
|---|---|
len(list) |
Number of elements |
append(list, value) |
Add element to the end |
pop(list) |
Remove and return the last element |
pop(list, index) |
Remove and return element at index |
insert(list, index, value) |
Insert value at index, shifting elements right |
sum(list) |
Sum all numeric elements |
List dot methods¶
| Method | Description |
|---|---|
list.append(value) |
Add element to the end |
list.pop() |
Remove and return the last element |
list.pop(index) |
Remove and return element at index |
list.len() |
Number of elements |
list.contains(value) |
True if value is in the list |
list.join(sep) |
Join elements into a string |
list.sort() |
Sort in place (ascending) |
list.reverse() |
Reverse in place |
nums = [3, 1, 4, 1, 5, 9]
nums.sort()
write(nums) # [1, 1, 3, 4, 5, 9]
nums.reverse()
write(nums) # [9, 5, 4, 3, 1, 1]
write(nums.contains(4)) # true
write(nums.len()) # 6
Strings¶
Strings support indexing and negative indices:
Strings also support slicing:
Membership test:
String dot methods¶
| Method | Description |
|---|---|
s.len() |
String length |
s.trim() |
Remove surrounding whitespace |
s.uppercase() |
Convert to uppercase |
s.lowercase() |
Convert to lowercase |
s.swap(old, new) |
Replace all occurrences of old with new |
s.split(sep?) |
Split into a list (default sep: whitespace) |
title = " Hello, World! "
write(title.trim()) # Hello, World!
write(title.trim().lowercase()) # hello, world!
write("a,b,c".split(",")) # ["a", "b", "c"]
write(title.trim().len()) # 13
Methods can be chained:
Dictionaries¶
A dictionary maps keys to values. Keys must be strings or numbers.
Generic type annotations¶
prices: dict[string, float] = {"apple": 1.5, "banana": 0.8}
counts: dict[string, int] = {"a": 1, "b": 2}
Access and assignment¶
Membership test¶
Iteration¶
Iterating over a dictionary yields its keys:
Dictionary built-ins¶
| Function | Description |
|---|---|
len(dict) |
Number of key-value pairs |
keys(dict) |
Returns a list of all keys |
values(dict) |
Returns a list of all values |
remove(dict, key) |
Remove key and return its value |
Dictionary dot methods¶
| Method | Equivalent built-in |
|---|---|
dict.keys() |
keys(dict) |
dict.values() |
values(dict) |
dict.len() |
len(dict) |
dict.contains(key) |
key in dict |
dict.remove(key) |
remove(dict, key) |