If we wanted to add a new key-value to a dictionary, we could use the following subscript syntax:

dictionaryName[NewKey] = NewValue

It is important to note that when adding new dictionary elements, the new key and value must match the data types of the dictionary it is being added to or else we will get an error.

Let’s say we created a dictionary donutPrices:

var donutPrices = [ "Strawberry": 2, "Chocolate": 3 ]

We could add a third element to donutPrices using the following code:

donutPrices["Boston Cream"] = 4

If we were to print() the value of donutPrices our output would be something like:

["Strawberry": 2, "Chocolate": 3, "Boston Cream": 4]



Take a look at the dictionary teaSteepingTemperature in TeaTemperature.swift to see the correct temperature to steep different teas.

Add the following key-value pairs to teaSteepingTemperature:

  • "Green": 185
  • "Rooibos": 212

Use print() to output teaSteepingTemperature.

Take this course for free

Mini Info Outline Icon
By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.

Or sign up using:

Already have an account?