.remove()

shaeeb0793657240's avatar
Published Oct 15, 2023Updated Oct 22, 2024
Contribute to Docs

The .remove() function in Lua removes the value of the specified index and clears the index from the table (reducing the length of the table by 1).

Syntax

table.remove(tableName, pos)

The .remove() method removes and returns the element at the specified position pos from the given table tableName. If pos is not provided, it defaults to the length of the table, meaning the last element will be removed by default.

Example

In the following example .remove() is used to remove the element at the pos position from the table. This causes other elements to shift down:

local fruit = {
"šŸŽ",
"šŸŒ",
"šŸ‡",
"šŸ“",
"šŸ‰"
}
local removedFruit = table.remove(fruit, 2);
print(removedFruit)
print(table.concat(fruit,", "))

This example results in the following output:

šŸŒ
šŸŽ, šŸ‡, šŸ“, šŸ‰

Note: In Lua, indices start from 1. Therefore the banana was removed from the table as it was at index 2.

All contributors

Contribute to Docs

Learn Lua on Codecademy