Lookup Tables

One common application of dictionaries is to create lookup tables. Let’s say that you have several objects, and each one has a unique identifier assigned to it. Assume that your code has to frequently look up characteristics of the objects based on their identifier. This can be easily done with a dictionary.

The code below illustrates how you might use a dictionary to store ID-Name pairs in a student database.

student_lookup = {
    601814: 'Brown, Mary',
    716978: 'Green, John',
    617945: 'Jones, Lana',
    863794: 'Smith, Hank',
    816773: 'Green, John'
}

We will now use the lookup table to find the names of two students based on their student ID numbers.

print(student_lookup[617945])
print(student_lookup[816773])
Jones, Lana
Green, John