Main Content

Create and Manage Dictionaries

Since R2022b. Recommended over containers.Map.

A dictionary stores data as key-value pairs. Each key is unique and maps to a corresponding value. Keys and values can be of different data types, and each key-value pair is an entry. You can look up or update values using their keys in constant time, regardless of the number of entries stored in the dictionary. Efficient lookups and updates make dictionaries useful for organizing large data sets where you can associate meaningful keys with values.

Create Dictionary

You can create a dictionary using the dictionary function. For example, create a dictionary that has product names as the keys and their prices as the associated values. Specify the keys and values as vectors. The dictionary output indicates the data types of the keys and values.

products = ["Tomato" "Carrot" "Mango" "Mushroom"];
prices = [1 0.5 2.50 1.99];
d = dictionary(products,prices)
d =

  dictionary (string ⟼ double) with 4 entries:

    "Tomato"   ⟼ 1
    "Carrot"   ⟼ 0.5000
    "Mango"    ⟼ 2.5000
    "Mushroom" ⟼ 1.9900

Alternatively, you can specify each key-value pair explicitly to create the dictionary.

d = dictionary("Tomato",1, ...
               "Carrot",0.5, ...
               "Mango",2.5, ...
               "Mushroom",1.99)
d =

  dictionary (string ⟼ double) with 4 entries:

    "Tomato"   ⟼ 1
    "Carrot"   ⟼ 0.5000
    "Mango"    ⟼ 2.5000
    "Mushroom" ⟼ 1.9900

Create Empty Dictionary

You can also start with an empty dictionary. This command creates an empty unconfigured dictionary, with no data types assigned to the keys and values.

d = dictionary
d =

  dictionary with unset key and value types.

You can then add entries to the dictionary. Adding entries to the unconfigured dictionary sets a data type for the keys and a data type for the values.

d("Tomato") = 1;
d("Carrot") = 0.5;
d("Mango") = 2.5;
d("Mushroom") = 1.99
d =

  dictionary (string ⟼ double) with 4 entries:

    "Tomato"   ⟼ 1
    "Carrot"   ⟼ 0.5000
    "Mango"    ⟼ 2.5000
    "Mushroom" ⟼ 1.9900

If you want to define the types for the keys and values first, create an empty configured dictionary by using the configureDictionary function. For example, products have type string and prices have type double.

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

  dictionary (string ⟼ double) with no entries.

Before R2023b A dictionary can be configured without adding any entries by creating empty inputs of the intended data types. For example, dictionary(string.empty,double.empty)**.

With a configured dictionary, MATLAB® converts compatible entries to the configured key and value types.

d("Tomato") = int8(1);
d("Carrot") = "0.5";
d("Mango") = single(2.5);
d("Mushroom") = 1.99
d =

  dictionary (string ⟼ double) with 4 entries:

    "Tomato"   ⟼ 1
    "Carrot"   ⟼ 0.5000
    "Mango"    ⟼ 2.5000
    "Mushroom" ⟼ 1.9900

Look Up Dictionary Values

After creating a dictionary, you can look up values using their associated keys. For example, look up the price of "Carrot".

d("Carrot")
ans = 
0.5000

You can also look up the prices for "Carrot" and "Mushroom" simultaneously.

d(["Carrot" "Mushroom"])
ans = 1×2

    0.5000    1.9900

Modify Dictionary Entries

Entries in a dictionary maintain the order in which they were added. Updating the value of an existing entry does not change its position. Each new entry is added to the end of the previous entries.

You can change an entry by mapping a new value to an existing key. For example, change the price of "Carrot" to 0.7.

d("Carrot") = 0.7
d =

  dictionary (string ⟼ double) with 4 entries:

    "Tomato"   ⟼ 1
    "Carrot"   ⟼ 0.7000
    "Mango"    ⟼ 2.5000
    "Mushroom" ⟼ 1.9900

To add new entries to a dictionary, you can map a new value to a new key.

For example, add the key "Potato" with a value of 0.75.

d("Potato") = 0.75
d =

  dictionary (string ⟼ double) with 5 entries:

    "Tomato"   ⟼ 1
    "Carrot"   ⟼ 0.7000
    "Mango"    ⟼ 2.5000
    "Mushroom" ⟼ 1.9900
    "Potato"   ⟼ 0.7500

Remove entries by assigning the keys to []. For example, remove the entry for "Mango".

d("Mango") = []
d =

  dictionary (string ⟼ double) with 4 entries:

    "Tomato"   ⟼ 1
    "Carrot"   ⟼ 0.7000
    "Mushroom" ⟼ 1.9900
    "Potato"   ⟼ 0.7500

Modify Multiple Dictionary Entries

You can modify multiple dictionary entries at the same time using vector operations. For example, update the prices of "Carrot" and "Mushroom" simultaneously.

d(["Carrot" "Mushroom"]) = [0.60 1.89]
d =

  dictionary (string ⟼ double) with 4 entries:

    "Tomato"   ⟼ 1
    "Carrot"   ⟼ 0.6000
    "Mushroom" ⟼ 1.8900
    "Potato"   ⟼ 0.7500

Add two new entries for "Celery" and "Grapes" with associated prices.

d(["Celery" "Grapes"]) = [0.50 1.95]
d =

  dictionary (string ⟼ double) with 6 entries:

    "Tomato"   ⟼ 1
    "Carrot"   ⟼ 0.6000
    "Mushroom" ⟼ 1.8900
    "Potato"   ⟼ 0.7500
    "Celery"   ⟼ 0.5000
    "Grapes"   ⟼ 1.9500

Add two new entries for "Apple" and "Pear" with the same price using scalar expansion.

d(["Apple" "Pear"]) = 1.14 
d =

  dictionary (string ⟼ double) with 8 entries:

    "Tomato"   ⟼ 1
    "Carrot"   ⟼ 0.6000
    "Mushroom" ⟼ 1.8900
    "Potato"   ⟼ 0.7500
    "Celery"   ⟼ 0.5000
    "Grapes"   ⟼ 1.9500
    "Apple"    ⟼ 1.1400
    "Pear"     ⟼ 1.1400

Remove the entries for "Apple","Pear", and "Grapes".

d(["Apple" "Pear" "Grapes"]) = []
d =

  dictionary (string ⟼ double) with 5 entries:

    "Tomato"   ⟼ 1
    "Carrot"   ⟼ 0.6000
    "Mushroom" ⟼ 1.8900
    "Potato"   ⟼ 0.7500
    "Celery"   ⟼ 0.5000

Merge Dictionaries

You can merge two dictionaries if their key types and value types are respectively identical or convertible and the value type supports concatenation. For example, create a new dictionary with dairy products.

d2 = dictionary("Milk",3.49,"Yogurt",0.99,"Cheese",4.25)
d2 =

  dictionary (string ⟼ double) with 3 entries:

    "Milk"   ⟼ 3.4900
    "Yogurt" ⟼ 0.9900
    "Cheese" ⟼ 4.2500

Merge the new dictionary d2 into the previous dictionary d.

d(keys(d2)) = values(d2)
d =

  dictionary (string ⟼ double) with 8 entries:

    "Tomato"   ⟼ 1
    "Carrot"   ⟼ 0.6000
    "Mushroom" ⟼ 1.8900
    "Potato"   ⟼ 0.7500
    "Celery"   ⟼ 0.5000
    "Milk"     ⟼ 3.4900
    "Yogurt"   ⟼ 0.9900
    "Cheese"   ⟼ 4.2500

Read and Write Dictionary to File

You can save a dictionary to a MAT file or JSON file and load the dictionary from that file into MATLAB.

Write Dictionary to File

You can write a dictionary to a MAT file. For example, save the dictionary d to a file named "mydict.mat".

save("mydict","d")

You can also write the dictionary to a JSON file.

writedictionary(d,"mydict.json")

Read Dictionary From File

Load the dictionary from the MAT file into MATLAB.

clear d
load("mydict.mat","d")
d
d =

  dictionary (string ⟼ double) with 8 entries:

    "Tomato"   ⟼ 1
    "Carrot"   ⟼ 0.6000
    "Mushroom" ⟼ 1.8900
    "Potato"   ⟼ 0.7500
    "Celery"   ⟼ 0.5000
    "Milk"     ⟼ 3.4900
    "Yogurt"   ⟼ 0.9900
    "Cheese"   ⟼ 4.2500

Alternatively, load the dictionary by reading the data from the JSON file.

d4 = readdictionary("mydict.json")
d4 =

  dictionary (string ⟼ double) with 8 entries:

    "Tomato"   ⟼ 1
    "Carrot"   ⟼ 0.6000
    "Mushroom" ⟼ 1.8900
    "Potato"   ⟼ 0.7500
    "Celery"   ⟼ 0.5000
    "Milk"     ⟼ 3.4900
    "Yogurt"   ⟼ 0.9900
    "Cheese"   ⟼ 4.2500

See Also

| | |

Topics