3

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?

jpp
  • 159,742
  • 34
  • 281
  • 339
noam compsci
  • 33
  • 1
  • 6
  • 2
    It is the development area. Ive had experiences with codecademy and their system only detects thats its right when its perfectly correct how they wrote it. It works perfectly in idle however. You might just want to skip. – Mercury Platinum May 17 '18 at 21:15
  • Did one of the answers below help? If so, consider [accepting](https://stackoverflow.com/help/someone-answers) (green tick on left), or ask for clarification. – jpp May 24 '18 at 23:04

4 Answers4

6

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.

MoxieBall
  • 1,907
  • 7
  • 23
  • Dang, beat me to it. To reiterate you need to return the variables you created. In this answer you are returning a list of dictionaries – SPYBUG96 May 17 '18 at 21:14
  • Though this certainly works, you might want to rename the `names = ...` variable inside the loop to be something else, just to avoid confusion given that the OP is new to Python. – miradulo May 17 '18 at 21:16
  • Thank you! In fact the very next exercise was actually to make the dictionaries into a list and so this is brilliant! Really appreciate it :) – noam compsci May 17 '18 at 21:38
3

What is the automated way to make a dictionary, based on lists of inputs?

You can use a dictionary comprehension here. A generator can avoid the need for boilerplate list construction code. In this solution, we yield items for each name in a list. Calling list then exhausts the generator, and we can assign the list to the variable my_dicts.

def make_dict(lst):
    for name in lst:
        d = {k: [] for k in ('homework', 'quizzes', 'tests')}
        d['name'] = name
        yield d

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

my_dicts = list(make_dict(list_of_names))
jpp
  • 159,742
  • 34
  • 281
  • 339
  • Nice and clean for someone who really knows what they're doing but using fromkeys and yield in response to a question about a beginner python course might be a bit more opaque than necessary. I didn't downvote though, I like it – MoxieBall May 17 '18 at 21:20
  • @MoxieBall, Sorry, `dict.fromkeys` won't work here as the same list will be referenced by each key. But a `dict` comprehension works too and, in my opinion, is readable. Generators, in my opinion, are underused, but conceptually straightforward. – jpp May 17 '18 at 21:24
  • Thanks for this. It seems like an advanced answer to my question and I will add this to my 'read when I am better' list - really appreciate the growth suggestion! – noam compsci May 17 '18 at 21:26
  • @noamcompsci, Sure, IMO it's always a good thing (when you learn about comprehensions and generators) to go back and see how you can approach a problem differently. – jpp May 17 '18 at 21:27
  • @jpp thanks for updating, I couldn't have told you at a glance that your original answer was wrong, definitely expanding my knowledge of how to leverage dictionaries – MoxieBall May 17 '18 at 21:40
2

You are creating three dictionaries; however, each one overwrites the previous one by assigning to the same variable names, and the last one is garbage collected because the only reference to it is a local variable that goes out of scope when make_dict returns.

You just need to return the created dict. For this exercise, it doesn't sound like you really need a loop.

def make_dict(name):
    return {
      "name": name,
      "homework" : [],
      "quizzes" : [],
      "tests" : []
    }

lloyd = make_dict("lloyd")
alice = make_dict("alice")
tyler = make_dict("tyler")
chepner
  • 497,756
  • 71
  • 530
  • 681
-1
Vegetables={"tamato":40,"carrot":50,"onion":60,"green chillies":20,"red chillies":40,"capsicum":20,"radish":30,"drumstick":40,"beetroot":50,"peas":90}
Print(vegetables)
Elletlar
  • 3,136
  • 7
  • 32
  • 38