Main Content

dictionary

Dictionary that maps unique keys to values

Since R2022b. Recommended over containers.Map.

Description

A dictionary is a map that stores data as values, which can be accessed using corresponding unique keys. Each pair of keys and values is an entry. Use a dictionary to efficiently look up values associated with a key.

Creation

Description

d = dictionary(keys,values) creates a dictionary with specified keys and values. The resulting dictionary d is a 1-by-1 scalar object. If you assign multiple values to the same key, then only the last of those values is assigned. New assignments to an existing key overwrite the value for that entry.

keys and values must be the same size unless values is a scalar, where each element of keys becomes a key for values. When keys and values are arrays, the number of entries is equal to the number of key-value pairs.

keys and values can be of any type, but key types and value types must be uniform. All keys and all values in a dictionary must share respective data types or be convertible to the configured data type. If parts of a new entry do not share the configured data types, then MATLAB® attempts to convert the entry types. Keys and values do not need to be of the same data type. dictionary converts keys and values specified as character row vectors to string scalars.

To store a mix of key or value types, use a cell array. When you perform a lookup on a dictionary that uses cells as values, the dictionary returns a cell array. You can access the contents of the cell array directly by using curly braces {} instead of parentheses. (since R2023a)

example

d = dictionary(k1,v1,...,kN,vN) creates a dictionary with specified key-value pairs. If you specify multiple instances of the same key, then only the last key-value pair is assigned.

d = dictionary creates an unconfigured dictionary with no keys or values.

When you create a dictionary without inputs, it is unconfigured and has no types. Adding entries to an unconfigured dictionary specifies a data type for the keys and a data type for the values.

example

Input Arguments

expand all

Keys, specified as a scalar, an array, or a cell array. Individual elements of keys must be scalars of the same data type or of compatible data types.

If keys is an array, each element creates a new key. If keys contains duplicate elements, the corresponding value of the last duplicate element is assigned.

Values, specified as a scalar, an array, or a cell array. Individual elements of values must be scalars of the same data type. If values need to be heterogeneous or nonscalar, use a cell array.

If values is an array, each element creates a new value. If keys is an array and values is a scalar, then that value is mapped to each key in keys.

Key-value pairs, specified as separate arguments of key and value scalars and arrays. All key arguments must have the same data type or have compatible data types. All value arguments must have the same data type or have compatible data types.

Output Arguments

expand all

Dictionary, returned as a dictionary object. Entries are stored in the order that they were added.

Usage

Use dictionary to create the dictionary d. Then you can access or change d using any of the following syntaxes.

Description

valueOut = d(keys) looks up the value associated with keys.

d(keys) = newValues assigns the elements of newValues to the entries specified by the corresponding values of keys. If a specified key does not exist in the dictionary, then a new entry is added. If you assign multiple values to the same key, then only the last of those values is assigned. New assignments to an existing key overwrite the value for that entry.

d(keys) = [] removes the entry associated with keys from the dictionary.

valueOut = d{keys} looks up the value associated with keys and returns the contents of the cell. If keys is an array, a comma-separated list of the corresponding values is returned. If the dictionary values are configured to be a data type other than cell, this syntax throws an error.

d{keys} = newValues assigns cells containing the elements of newValues to the entries specified by the corresponding values of keys. If the dictionary values are configured to be a data type other than cell, this syntax throws an error.

Object Functions

configureDictionaryCreate dictionary with specified key and value types
insertAdd entries to a dictionary
lookupFind value in dictionary by key
removeRemove dictionary entries
entriesKey-value pairs of dictionary
keysKeys of dictionary
valuesValues of dictionary
typesTypes of dictionary keys and values
numEntriesNumber of key-value pairs in dictionary
isConfiguredDetermine if dictionary has types assigned to keys and values
isKeyDetermine if dictionary contains key

Examples

collapse all

Create a dictionary that maps different vehicle names to the number of wheels.

Create an array of names and an array of corresponding number of wheels.

names = ["Unicycle" "Bicycle" "Tricycle"];
wheels = [1 2 3];

Create a dictionary using the names as keys and the number of wheels as values.

d = dictionary(names,wheels)
d =

  dictionary (string ⟼ double) with 3 entries:

    "Unicycle" ⟼ 1
    "Bicycle"  ⟼ 2
    "Tricycle" ⟼ 3

Access dictionary values by using the key as an index.

d("Tricycle")
ans = 
3

Modify a dictionary entry by assigning a new value to an existing key.

d("Bicycle") = 2.5
d =

  dictionary (string ⟼ double) with 3 entries:

    "Unicycle" ⟼ 1
    "Bicycle"  ⟼ 2.5000
    "Tricycle" ⟼ 3

Add a new entry to the dictionary by assigning a value to a new key.

d("Car") = 4
d =

  dictionary (string ⟼ double) with 4 entries:

    "Unicycle" ⟼ 1
    "Bicycle"  ⟼ 2.5000
    "Tricycle" ⟼ 3
    "Car"      ⟼ 4

Add multiple entries using arrays of keys and values.

names2 = ["Truck" "Motorcycle" "Sixteen-Wheeler"];
wheels2 = [4 2 16];
d(names2) = wheels2
d =

  dictionary (string ⟼ double) with 7 entries:

    "Unicycle"        ⟼ 1
    "Bicycle"         ⟼ 2.5000
    "Tricycle"        ⟼ 3
    "Car"             ⟼ 4
    "Truck"           ⟼ 4
    "Motorcycle"      ⟼ 2
    "Sixteen-Wheeler" ⟼ 16

Remove an entry by assigning an empty array to an existing key.

d("Truck") = []
d =

  dictionary (string ⟼ double) with 6 entries:

    "Unicycle"        ⟼ 1
    "Bicycle"         ⟼ 2.5000
    "Tricycle"        ⟼ 3
    "Car"             ⟼ 4
    "Motorcycle"      ⟼ 2
    "Sixteen-Wheeler" ⟼ 16

The dictionary automatically converts new entries to match the configured data types. If a conversion is not possible, MATLAB throws an error.

d('Spider-Car') = "8"
d =

  dictionary (string ⟼ double) with 7 entries:

    "Unicycle"        ⟼ 1
    "Bicycle"         ⟼ 2.5000
    "Tricycle"        ⟼ 3
    "Car"             ⟼ 4
    "Motorcycle"      ⟼ 2
    "Sixteen-Wheeler" ⟼ 16
    "Spider-Car"      ⟼ 8

Dictionary values must have the same type. However, data of different types can be stored in a dictionary as a cell.

Create a cell array that contains values of various data types and a string array of keys.

myValues = {datetime,@myfun,struct,[1 2 3 4]}
myValues=1×4 cell array
    {[24-Jan-2026 19:18:05]}    {@myfun}    {1×1 struct}    {[1 2 3 4]}

myKeys = ["my birthday" "my favorite function" "a structure" "numeric array"]
myKeys = 1×4 string
    "my birthday"    "my favorite function"    "a structure"    "numeric array"

Create a dictionary using the specified keys and values.

d = dictionary(myKeys,myValues)
d =

  dictionary (string ⟼ cell) with 4 entries:

    "my birthday"          ⟼ {[24-Jan-2026 19:18:05]}
    "my favorite function" ⟼ {@myfun}
    "a structure"          ⟼ {1×1 struct}
    "numeric array"        ⟼ {[1 2 3 4]}

Extract values as a cell array using the values function.

values(d)
ans=4×1 cell array
    {[24-Jan-2026 19:18:05]}
    {                @myfun}
    {1×1 struct            }
    {[             1 2 3 4]}

Look up the contents of cells stored as values directly using curly braces {}. (since R2023a)

d{"numeric array"}
ans = 1×4

     1     2     3     4

Similarly, you can add new entries of any data type into an existing dictionary with cell values using curly braces {}.

d{"a new entry"} = table
d =

  dictionary (string ⟼ cell) with 5 entries:

    "my birthday"          ⟼ {[24-Jan-2026 19:18:05]}
    "my favorite function" ⟼ {@myfun}
    "a structure"          ⟼ {1×1 struct}
    "numeric array"        ⟼ {[1 2 3 4]}
    "a new entry"          ⟼ {0×0 table}

Create an empty configured dictionary using configureDictionary.

d = configureDictionary("string","double")
d =

  dictionary (string ⟼ double) with no entries.

Before R2023b: Assign empty inputs of the desired types. For example, d = dictionary(string([]),[]).

You can add new dictionary entries as long as the data types match or can be converted to the data types of the configured dictionary.

d("Unicycle") = 1;
d("Bicycle") = 2;
d("Tricycle") = 3
d =

  dictionary (string ⟼ double) with 3 entries:

    "Unicycle" ⟼ 1
    "Bicycle"  ⟼ 2
    "Tricycle" ⟼ 3

Create an unconfigured dictionary by calling dictionary without inputs.

d = dictionary
d =

  dictionary with unset key and value types.

When you add entries to an unconfigured dictionary, MATLAB configures the dictionary using the data types of the entries.

names = ["Unicycle" "Bicycle" "Tricycle"];
wheels = [1 2 3];
d(names) = wheels
d =

  dictionary (string ⟼ double) with 3 entries:

    "Unicycle" ⟼ 1
    "Bicycle"  ⟼ 2
    "Tricycle" ⟼ 3

Tips

  • You can use custom classes for both keys and values. However, if you use classes with overloaded indexing or size queries or whose behavior differs from standard array behavior, the dictionary might not behave as expected. For additional information, see Dictionaries and Custom Classes.

  • Entries are stored in the order that they are added and returned in the same order. For example, create a dictionary with three entries:

    d = dictionary("first",1,"second",2,"third",3)
    d =
    
      dictionary (string ⟼ double) with 3 entries:
    
        "first"  ⟼ 1
        "second" ⟼ 2
        "third"  ⟼ 3

    The keys or values from this dictionary are returned in the same order in which they were added.

    k = keys(d)
    k = 
    
      3×1 string array
    
        "first"
        "second"
        "third"

    If additional entries are added, they are added to the end of previous entries.

    d("fourth") = 4
    d =
    
      dictionary (string ⟼ double) with 4 entries:
    
        "first"  ⟼ 1
        "second" ⟼ 2
        "third"  ⟼ 3
        "fourth" ⟼ 4

    Updating the value of an entry does not change its place in the order of entries.

Extended Capabilities

expand all

Version History

Introduced in R2022b

expand all