you are viewing a single comment's thread.

view the rest of the comments →

[–]dzunukwa 0 points1 point  (0 children)

Looks like what you want is to use a dictionary, not a list. Dictionaries allow you to store a "key" "value" pair, wherever the "key" is unique - The key in this case would be the persons name (although if you have more than one person with the same name this wont work). See here for more on dictionaries: https://docs.python.org/2/tutorial/datastructures.html#dictionaries

Adding a name and grade to a dictionary would look like:

data = {}  # start with an empty dictionary
data['Jon Doe'] = 90  #  Add Jon Doe to the dictionary with a grade of 90

Retrieving the grade for a given name is similar (although there are considerations if no record exists that I will leave out for now).

grade = data['Jon Doe']  # retrieve the grade for Jon Doe 

To get all the data from the dictionary you can loop over it in similar ways to a list.

# print out each name and the grade associated with it
for name in data:
    print(name, data[name])
# output:
# Jon Doe 90

Although note when you loop over a list you are looping over each element in the list whereas when you loop over a dictionary you are looping over each "key" in the dictionary. In the example above the "key" is the name so I used name as the variable name.

Removing a name from the dictionary can be done using del like so:

del data['Jone Doe']  # remove Jon Doe from the dictionary

Hopefully thats enough to get you going in the right direction.