Dictionaries
Dictionaries allow for the storage of data in the form of key-value
pairs. Keys
are unique dictionary elements that are assigned a value
. A key-value pair
is termed as an item
. You can consider keys as indices
of a dictionary. That is, the values stored in a dictionary can be accessed using a key
. Moreover, unlike lists, dictionaries can store a collection of elements (using a list or tuple) as a value for a single key.
Let's look at few examples of dictionaries in Python.
1student = {"ID": "01", "name":"Patrick", "age":18, "major":"Biology"}
Visualizing this example, we see that the data is stored somewhat like the illustration below.

If we want to store multiple values for a single key, all the values are grouped in a list (or a tuple) and assigned to the corresponding key.
1students = {"ID": ["01", "02", "03", "04"], "name":["Patrick", "William", "Elizabeth", "John"], "age":[18, 19, 19, 20], "major":["Biology", "CS", "Literature", "Mathematics"]}
Storing multiple values within a single key stores the data in the form of tables within the program.

To access values in a dictionary, keys are used as indices with the dictionary.
1student = {"ID": "01", "name":"Patrick", "age":18, "major":"Biology"}
2
3print(student["name"])