remove

Deletes an entry from a dictionary or a containers.Map by key.

Category: Language Fundamentals > Data Types

Syntax

D = remove(D, key)

Returns the dictionary D without the entry for key.

remove(M, key)

Deletes key from the containers.Map M, in place.

Description

A dictionary behaves like an ordinary value, so remove returns a new dictionary and the original is unchanged until you assign the result. A containers.Map is a handle object: remove changes the map itself.

Asking to remove a key that does not exist is an error for both types; test with isKey first if the key may be missing.

Input arguments

D / M
A dictionary or a containers.Map object.
key
A key, or an array of keys, to delete.

Output arguments

D
The updated dictionary. Dictionaries are values, so the result must be assigned.

Example

d = dictionary(["a" "b"], [1 2]);
d = remove(d, "a")
m = containers.Map({'a','b'}, [1 2]);
remove(m, 'a');
m.keys
Output
d = 
  dictionary (string --> double) with 1 entry:

    "b" --> 2
ans = 1×1 cell

    {'b'}

See also

dictionary containers.Map isKey keys numEntries values