Lists in Kiwi¶
Lists are dynamic, ordered, mutable sequences that can hold values of any type (including other lists, hashmaps, etc.). They grow automatically as needed and support slicing, and many useful built-in methods.
Quick Reference – Common Operations¶
| Operation | Syntax / Method | Returns | Mutates? |
|---|---|---|---|
| Create | [], [1, 2, 3], [1 to 10] |
list |
— |
| Get element | lst[3], lst[0] |
value | No |
| Set element | lst[2] = "new" |
— | Yes |
| Append | lst.push(value), lst.concat(values) |
— | Yes |
| Remove by index | lst.remove_at(idx) |
removed value | Yes |
| Remove by value | lst.remove(value) |
— | Yes |
| Filter | lst.filter(λ) |
new list | No |
| Map | lst.map(λ) |
new list | No |
| Length | lst.size() or lst.length() |
integer | No |
| Iterate | for item in lst do … |
— | — |
| Iterate with index | for item, idx in lst do … |
— | — |
For the full list of built-in methods, see List Builtins.
Creating Lists¶
Literal syntax¶
From a range (very common idiom)¶
See Ranges.
Accessing & Modifying Elements¶
Indices start at 0..
colors = ["red", "green", "blue", "yellow"]
println colors[0] # "red"
println colors[2] # "blue"
println colors[3] # "yellow" (last)
# Modify in place
colors[1] = "emerald"
println colors # ["red", "emerald", "blue", "yellow"]
Out-of-bounds behavior
Reading past the end throws IndexError.
Writing past the end throws an error (use .push() to grow).
Adding Elements¶
Append (most common)¶
tasks = ["write docs", "refactor"]
tasks.push("add tests")
tasks.push(42) # lists can be heterogeneous
println tasks.size() # 4
Append multiple¶
more = ["deploy", "celebrate"]
tasks.push(more) # adds the whole list as one element
# ["write docs", "refactor", "add tests", 42, ["deploy", "celebrate"]]
tasks.concat(more) # use concat to combine two lists
# ["write docs", "refactor", "add tests", 42, "deploy", "celebrate"]
Removing Elements¶
By index¶
letters = "abcdef".chars() # ["a","b","c","d","e","f"]
letters.remove_at(3) # removes "d"
println letters # ["a","b","c","e","f"]
By value (removes first occurrence)¶
Remove all occurrences¶
Transforming Lists (non-destructive)¶
Filter¶
words = ["kiwi", "apple", "banana", "mango", "lime"]
short = words.filter(do (w) => w.size() <= 4)
println short # ["kiwi", "lime"]
Map¶
Chaining¶
result = [1 to 10]
.filter(do (n) => n % 2 == 1) # odds
.map(do (n) => n * 10) # ×10
println result # [10, 30, 50, 70, 90]
A list literal can also be used directly as a statement — calling a method on it without assigning the result, or combined with a when guard:
Note on line breaks and
[: A[at the start of a new line is always parsed as a new list literal, never as an index operator on the expression above it. Same-line indexing (lst[i]) is unaffected.
Iterating Lists¶
Basic iteration¶
With index¶
With guard clauses (idiomatic)¶
Full Example – Word frequency¶
text = "kiwi is fun kiwi is awesome kiwi wins"
words = text.split(" ")
counts = {}
for word in words do
word = word.lowercase()
next when word.empty()
counts[word] = counts.get(word, 0) + 1
end
println counts
# { "kiwi": 3, "is": 2, "fun": 1, "awesome": 1, "wins": 1 }
Best Practices & Tips¶
- Chain
.filter(),.map(),.take(), etc. for clean data pipelines - Avoid modifying a list while iterating over it with
for— use indices or.clone() - For very large lists, prefer iterators
ListIteratorwhen you only need sequential access
See also:
- List Builtins
- Ranges
- Hashmaps
- Loops – for