Converting a List¶

You may have used a list before to store a lot of different information about a particular thing. As you were using it, chances are you forgot what index position a particular piece of info was located.

Download the code and get it to run:

What you should see¶

Name: Jim
Age: 54
Eye colour: ???
Favourite colour: ???

What you should do¶

Without clarifying comments accompanying that list, it is impossible to know for sure what the eye colour or favourite colour is. Comments are good, but clear, self-explanatory code is better.

  1. Convert this list to a dictionary. We will say their eye color is the "green" there. Notice how the key -> value pair structure of a dictionary makes it extremely clear what each piece of data is.

  2. Complete the print statements using the dictionary.

  3. Close your eyes (after reading this) and try to remember what index position the person’s age was at in the original list. No peaking. The fact we need to refer back to the original code to remind ourselves of this should be a clue that a list isn’t the greatest tool for holding different pieces of information about a single entity. Lists are, however, excellent at storing a lot of one type of thing, like a list of marks.


©2021 Daniel Gallo

This assignment is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 United States License.

Creative Commons License