mirror of
https://github.com/erg-lang/erg.git
synced 2025-08-04 18:58:30 +00:00
1.1 KiB
1.1 KiB
List
A list is the most basic collection (aggregate). A collection is an object that can hold multiple objects inside it.
a = [1, 2, 3]
a: [Int; 3] # Type specification: number after semicolon is the number of elements
# Can be omitted if the number of elements is not known
a: [Int]
mut_a = [!1, !2, !3]
mut_a[0].inc!()
assert mut_a == [2, 2, 3]
As a rule, lists cannot contain objects of different types.
[1, "a"] # TypeError: 1st element is Int, but 2nd element is Str
However, you can bypass the restriction by explicitly specifying the type like this.
[1, "a"]: [Int or Str; 2]
Slice
A list can also have multiple values taken out at once. This is called slicing.
l = [1, 2, 3, 4]
# Same as l[1:3] in Python
assert l[1.. <3] == [2, 3]
assert l[1..2] == [2, 3]
# Same as l[1]
assert l[1..1] == [2]
# Same as l[::2] in Python
assert l[..].step(2) == [2, 4]
The object obtained by slicing is an (immutable) copy to a list.
print! Typeof l[1..2] # [Int; 4]