Lesson 26 of 55
9 mins readPython Tables as 1-Based Arrays & Length (#)
In Plain English
When using tables as arrays, Lua convention uses 1-based indexing: `list[1]` is the first element. The length operator `#list` returns the number of elements in a sequence.
Deep Dive: How It Works
1-Based Indexing: Standard in Lua since its engineering roots at Petrobras/PUC-Rio.
Length Operator (#): Returns index `n` such that `t[n] ~= nil` and `t[n + 1] == nil`.
Appending Elements: `list[#list + 1] = new_value` is standard Lua idiom for appending.
Core Rules to Remember

1-Based Convention: First item is at index 1, last is at `#table`.

Append Idiom: `t[#t + 1] = val` appends to the end of a sequence.
Live Interactive Example
Hit Run Code to see it liveArray Indexing and Appending
Python 3.12
1
2
3
4
5
6
7
8
9
Output Console
Click "Run Code" to view the rendered output.
How it works: languages[1] accessed the first item and #languages[#languages + 1] appended Rust.
Your Turn: Micro Challenge
No pressure! Edit the starter code below and test your solution with instant feedback.
Micro Exercise
Append and Query Sequence
Declare `local list = { 10, 20 }`.
Append 30: `list[#list + 1] = 30`.
Print `"Count: " .. #list .. " Last: " .. list[#list]`.
1
2
Sandbox Output
Click "Run & Check" to test your solution.
Finished reading and practicing?
Mark this lesson as completed to update your course progress.