Start learning free

LESSON 131 OF 671 · DICTIONARIES

Deleting a Key from a Go Map

Lesson Read the full explanation

TL ; DR:

The built-in function delete removes a key and its value from a map.


toys := map[string]int{"ball": 3, "doll": 2, "kite": 1}
delete(toys, "kite")

fmt.Println(len(toys))   // 2


Full lesson:

Sometimes an entry has to go. A shop stops selling an item. A player leaves the game.

Call delete with the map, then the key. Like len, it is built into Go. The key and its value go away together.

The length drops by 1. Deleting a key that isn't there does nothing. There is no error.

Watch out: after the delete, toys["kite"] gives 0, the zero value. That looks like a count of 0 kites. Only the comma-ok form tells you the key is gone.

_, ok := toys["kite"]   // ok is false

Your task

A pet shop counts what it has for sale in animals. Each key is a kind of animal. Its value is how many the shop has.

The last rabbit just went home with a family. Delete the key "rabbit" from animals.

Then print len(animals) again. The code at the bottom checks if any rabbits are left.

Expected output:

3
2
No rabbits left
STEP 1 OF 2
    Ctrl + Enter to check

    GO · DICTIONARIES

    Deleting Dictionary Keys in Go, explained

    The delete function

    Sometimes an entry has to go. A shop stops selling an item, or a player leaves the game. Go has a built-in function for it: delete(m, key). It takes the map and then the key, and removes the key together with its value. Like len, it is built into the language, so it needs no import and no package name in front.

    animals := map[string]int{"cat": 4, "dog": 2, "rabbit": 1}
    delete(animals, "rabbit")
    fmt.Println(len(animals))   // 2
    

    The length drops by 1, because the key and its value are both gone. The call changes the map itself, so there is nothing to assign back, unlike append for a slice.

    Deleting a key that isn't there

    Deleting a missing key does nothing, and there is no error. After delete(animals, "rabbit") a second time, or delete(animals, "zebra"), the map still has 2 keys. Deleting from a nil map is also allowed. This is handy: you don't have to check the key before you delete it.

    It is also safe to call delete inside a for ... range loop over the same map. The entries you delete will not appear later in the loop. For example, deleting every even value from {"a": 1, "b": 2, "c": 3, "d": 4} leaves map[a:1 c:3].

    After the delete

    Watch out: once the rabbit is deleted, animals["rabbit"] gives 0, the zero value of an int. That looks like a rabbit count of 0, even though the key is gone. Only the comma-ok lookup tells you the key is not there:

    _, ok := animals["rabbit"]
    fmt.Println(ok)   // false
    

    This matters when you think of setting the value to 0 as a way to delete. After m["pen"] = 0 the key is still in the map, with a count of 0, and len(m) is still 1. If you want the entry gone, use delete.

    The task: the last rabbit

    A pet shop keeps animals, where each key is a kind of animal and each value is how many the shop has. It starts as cat: 4, dog: 2 and rabbit: 1. The last rabbit has just gone home with a family.

    1. Below the first fmt.Println, which prints 3, call delete(animals, "rabbit").
    2. Print len(animals) again. One key is gone, so it prints 2.

    The code at the bottom checks whether any rabbits are left with a comma-ok lookup. The key is gone, so ok is false and the else branch runs.

    animals := map[string]int{"cat": 4, "dog": 2, "rabbit": 1}
    fmt.Println(len(animals))
    delete(animals, "rabbit")
    fmt.Println(len(animals))
    if _, ok := animals["rabbit"]; ok {
        fmt.Println("We still have rabbits")
    } else {
        fmt.Println("No rabbits left")
    }
    

    The output is:

    3
    2
    No rabbits left
    

    Common mistakes

    • Writing animals = delete(animals, "rabbit"). The function returns nothing, so that doesn't compile.
    • Passing only the key. delete needs the map first, then the key.
    • Printing len before the delete and expecting the new number. The length changes only after the call.

    Key takeaways

    • delete(m, key) removes the key and its value, and the length drops by 1.
    • It changes the map in place; nothing is assigned back.
    • Deleting a missing key does nothing.
    • After a delete, a plain lookup gives the zero value; use comma-ok to see that the key is gone.
    Keep going with Go500+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free