Menu
Coddy logo textTech

table.remove()

Part of the Fundamentals section of Coddy's Lua journey. Lesson 62 of 90.

The table.remove() function to make this easy. This function removes an element from a list-style table and automatically adjusts the remaining elements.

The basic syntax for removing the last element is simple:

local items = {"sword", "shield", "potion"}
table.remove(items)  -- removes the last item
print(#items)  -- outputs: 2

When you call table.remove() without specifying a position, it removes the last element by default. The function also returns the removed value, so you can capture it if needed:

local inventory = {"apple", "bread", "water"}
local removedItem = table.remove(inventory)
print(removedItem)  -- outputs: water
challenge icon

Challenge

Easy

Create a backpack management system that demonstrates removing items from your adventure gear. First, create a table named backpack containing exactly four string values: "rope", "torch", "map", and "compass". After creating the table, use table.remove() to remove the last item from the backpack. Finally, print the length of the table using the # operator to confirm the item was successfully removed.

Try it yourself

-- TODO: Write your code here
-- Create the backpack table with the required items
-- Remove the last item using table.remove()
-- Print the length of the table using the # operator
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals

Practice on your own: Online Lua compiler