What are dictionaries? Dictionaries are another collection in Python.
They are different than keys:

a set which contains (key, value) pairs

dictionary
For every key in a dictionary, there is a value. Unlike lists, dictionaries do not have an specific order.

Related course: Complete Python Bootcamp: Go from zero to hero in Python

Example

You can create a dictionary with this line:
words = {}

Beware: This is just two characters different from a list.

Type the code below and run it:

#!/usr/bin/python

words = {}
words["BMP"] = "Bitmap"
words["BTW"] = "By The Way"
words["BRB"] = "Be Right Back"

print words["BMP"]
print words["BRB"]

Exercise

  1. Make a mapping from countries to country short codes
  2. Print each item (key and value)

Download answers