Menu
Coddy logo textTech

Removing Pairs with nil

Part of the Fundamentals section of Coddy's Lua journey — lesson 69 of 90.

When you assign nil to a key, Lua effectively removes that key-value pair from the table:

local settings = {
    ["volume"] = 80,
    ["difficulty"] = "normal",
    ["music"] = true
}

-- Remove the difficulty setting
settings.difficulty = nil

-- Now the difficulty key no longer exists in the table

You can use either dot notation or bracket notation to remove a key-value pair. Both settings.difficulty = nil and settings["difficulty"] = nil work exactly the same way.

After assigning nil to a key, that key completely disappears from the table. If you try to access it later, you'll get nil as the result, just as if the key never existed in the first place.

challenge icon

Challenge

Easy

Create a game configuration system that demonstrates removing unwanted settings from a dictionary-style table. Start by creating a table named gameConfig with the following key-value pairs: ["graphics"] should be assigned the string "high", ["sound"] should be assigned the number 75, ["difficulty"] should be assigned the string "normal", and ["debug"] should be assigned the boolean true.

After creating the initial configuration table, remove the debug setting by assigning nil to it using dot notation. Then print the remaining sound setting to verify the table still contains the other values.

Cheat sheet

To remove a key-value pair from a table, assign nil to the key:

local settings = {
    ["volume"] = 80,
    ["difficulty"] = "normal",
    ["music"] = true
}

-- Remove the difficulty setting
settings.difficulty = nil

You can use either dot notation or bracket notation:

settings.difficulty = nil
settings["difficulty"] = nil

After assigning nil, the key completely disappears from the table. Accessing it returns nil.

Try it yourself

-- TODO: Write your code here
-- Create the gameConfig table with the required key-value pairs
-- Remove the debug setting using dot notation
-- Print the sound setting
quiz iconTest yourself

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

All lessons in Fundamentals