pythonlistdictionaryautomated-deployment

How can I automate making dictionaries using python?


I am doing a beginners Python course and the aim is to make a bunch of dictionaries.

  1. Create three dictionaries: lloyd, alice, and tyler.

Give each dictionary the keys "name", "homework", "quizzes", and "tests".

Have the "name" key be the name of the student (that is, lloyd's name should be "Lloyd") and the other keys should be an empty list (We'll fill in these lists soon!)

I did this by doing the following:

def make_dict(list_of_names):
  for names in list_of_names:
    names = {
      "name": names,
      "homework" : [],
      "quizzes" : [],
      "tests" : []
  }

list_of_names = ["lloyd", 'alice', 'tyler']

make_dict(list_of_names)

Why does this not work? Should it work and is it just the Codeacademy development area that does not allow this to work? I realise I am being a little extra and that I could do this really straightforwardly and am purposely trying to be creative in how I do it.

In any case, what is the automated way to make a dictionary, based on lists of inputs?


Solution

  • You're creating a dictionary called names in each loop but not actually doing anything with it --

    def make_dict(list_of_names):
        results = []
        for names in list_of_names:
            names = {
                "name": names,
                "homework" : [],
                "quizzes" : [],
                "tests" : []
            }
            results.append(names)
        return results
    
    list_of_names = ["lloyd", 'alice', 'tyler']
    
    my_dicts = make_dict(list_of_names)
    

    This keeps track of the names dicts you have created, and then gives them to you at the end.