Dictionaries

Dictionaries are data structures that index values by a given key (key-value pairs).

Dictionaries are written with curly brackets {}, and they have keys and values.

The general syntax for creating a dictionary is:

DictionaryName {

key1: value1,
key2: value2,
.
.
.
keyN: valueN,
}


Every key in a dictionary must be unique so that we know which value to return for a given key; however, dictionaries are NOT sorted. What makes dictionaries useful is that we assign a key to each value, instead of a numerical index like we do for a list.

Here is the visual example of a dictionary named Student with keys as student names and values as student ages.

Get hands-on with 1000+ tech skills courses.