Quarry School

Choose a list or dictionary for a roster — quick review

In plain words

A class has a seating order and a room for each student.

Rule

Use a list when the order or position of values matters, and read it by index. Use a dictionary when values are looked up by a key such as a name, and read it by key. Keys of a dictionary are unique; values and list items may repeat.

Step

  1. Write the questions the program must answer.

  2. If a question asks by position, such as first or next, keep a list.

  3. If a question asks by a name or code, keep a dictionary with that name as the key.

  4. Check the keys: each key holds one value only.

  5. Use both when the program needs both kinds of question.

Trap

Using a name as a list index stops the program: seats["Cy"] raises a TypeError, because a list index is a whole number. Keep a dictionary for lookups by name.

Using one dictionary for values that share a key loses one of them: storing a second value with the same key replaces the first.