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: 2When 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: waterChallenge
EasyCreate 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.
Cheat sheet
Use table.remove() to remove elements from a list-style table:
local items = {"sword", "shield", "potion"}
table.remove(items) -- removes the last item by default
print(#items) -- outputs: 2The function returns the removed value:
local inventory = {"apple", "bread", "water"}
local removedItem = table.remove(inventory)
print(removedItem) -- outputs: waterTry 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 # operatorThis lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Operators 2 Relational & Logic
Equality OperatorsRelational OperatorsThe 'and' OperatorThe 'or' OperatorThe 'not' OperatorShort-Circuit EvaluationTruthy and Falsy ValuesRecap - Simple Logic7Basic Conditional Logic
The if-then StatementThe if-then-else StatementThe elseif StatementRecap - Treasure Chest10Tables Part 1: List-Style
What is a Table?Creating a TableAccessing Elements by IndexModifying Elements by IndexThe Length Operator '#'table.insert()table.remove()Recap - Manage Party Members2Variables and Data Types
What is a Variable?NumbersStringsBooleansThe Value 'nil'The type() FunctionNaming ConventionsRecap - Character Profile5Basic Output
Printing LiteralsPrinting VariablesPrinting Multiple ValuesCombining Strings & VariablesThe tostring() FunctionInputCastRecap - Status ReportRecap - Till 1208String Manipulation Basics
string.len()string.upper & string.lowerstring.sub()string.rep()string.find()Recap - Format Username3Operators 1 Arithmetic & Conc
Arithmetic OperatorsModulo OperatorExponentiation OperatorString ConcatenationOperator PrecedenceRecap - Simple Calculations6Project: Character Stats Disp
Welcome MessageDeclare Character Stats9Functions Basics
Declaring a FunctionCalling a FunctionFunctions with ParametersFunctions with Multiple ParamsThe 'return' StatementRecap - Area Calculator