0Pricing
Lua Academy · Lesson

Adding and Removing Items

Use table.insert and remove.

Adding and Removing Items is a free Lua Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Lua Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Growing a Sequence

Real lists change size while a program runs. Lua gives you table.insert to append items at the end.

Because sequences are 1-based, an append after two items lands at index 3.

local list = {"a", "b"}
table.insert(list, "c")
print(list[1], list[2], list[3])

table.insert Appends

Called with two arguments, table.insert(t, value) places value at the position right after the current last index.

Each call grows the sequence by one, keeping the indices contiguous.

local q = {}
table.insert(q, "first")
table.insert(q, "second")
print(q[1], q[2])

Insert at a Position

With three arguments, table.insert(t, pos, value) inserts at index pos and shifts later items up.

Here we put "X" at position 2, so the old second item slides to position 3.

local list = {"a", "b", "c"}
table.insert(list, 2, "X")
print(list[1], list[2], list[3], list[4])

Insert at the Front

To prepend, insert at position 1. Every existing element shifts one index higher.

This is handy but slower for big lists, since Lua must move all items.

local list = {"b", "c"}
table.insert(list, 1, "a")
print(list[1], list[2], list[3])

Removing the Last Item

table.remove(t) with one argument deletes and returns the last element.

The sequence shrinks by one and stays gap-free.

local list = {"a", "b", "c"}
local gone = table.remove(list)
print("removed:", gone)
print(list[1], list[2], list[3])

Remove at a Position

table.remove(t, pos) deletes the item at pos and shifts later items down to close the gap.

Remember positions are 1-based, so pos = 1 removes the first element.

local list = {"a", "b", "c"}
local gone = table.remove(list, 1)
print("removed:", gone)
print(list[1], list[2])

Remove Returns the Value

Both forms of table.remove return the value they took out, so you can use it right away.

This makes a table act like a stack: push with insert, pop with remove.

local stack = {}
table.insert(stack, 10)
table.insert(stack, 20)
local top = table.remove(stack)
print("popped:", top)

A Simple Queue

Insert at the end and remove from the front (pos = 1) to model a queue: first in, first out.

Each removal shifts the rest down, so the next caller waits at index 1.

local queue = {}
table.insert(queue, "Ann")
table.insert(queue, "Bob")
print("serving:", table.remove(queue, 1))
print("next:", queue[1])

Avoid Manual Holes

Setting an element to nil with list[2] = nil can leave a gap in the middle, breaking the sequence.

Prefer table.remove, which shifts items and keeps indices continuous.

local list = {"a", "b", "c"}
table.remove(list, 2)
print(list[1], list[2])

Building From Input

A common pattern: start with an empty table and append as data arrives.

Here we collect even numbers into a fresh sequence using insert.

local evens = {}
for n = 1, 6 do
  if n % 2 == 0 then
    table.insert(evens, n)
  end
end
print(evens[1], evens[2], evens[3])

Insert and Remove Together

You can mix operations freely. Below we append three names, then remove the middle one.

After removal the list has two contiguous items.

local names = {}
table.insert(names, "Al")
table.insert(names, "Bea")
table.insert(names, "Cy")
table.remove(names, 2)
print(names[1], names[2])

Quick Check

Think about how removal shifts the sequence.

Recap

You learned to grow lists with table.insert (append or insert at a 1-based position) and shrink them with table.remove, which returns the removed value and closes gaps.

Avoid manual nil holes. Next you will iterate cleanly with ipairs.

Frequently asked questions

Is the “Adding and Removing Items” lesson free?

Yes — the full text of “Adding and Removing Items” is free to read here on the web, and the Lua Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Lua Academy course, upgrade to CoddyKit PRO.

What will I learn in “Adding and Removing Items”?

Use table.insert and remove. You practise Lua Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Lua Academy?

No prior experience is required. Lua Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Adding and Removing Items” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Lua Academy lesson?

Yes. Every Lua Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Arrays as Tables
  2. Adding and Removing Items
  3. Iterating with ipairs
  4. Length and Sorting
← Back to Lua Academy