1/9 - A better way?



I wonder if there is a better way (using only Python skills acquired so far) to make these 3 dictionaries (in this exercise)?

Desired lists (outcome):

lloyd_stats = {"name": "lloyd", "homework": [], "quizzes": [], "tests": []}
alice_stats = {"name": "alice", "homework": [], "quizzes": [], "tests": []}
tyler_stats = {"name": "tyler", "homework": [], "quizzes": [], "tests": []}

My (failed) attempt:

student_list = ("lloyd", "alice", "tyler")
student_stats = {"name": [], "homework": [], "quizzes": [], "tests": []}

for student in student_list:
    student = {"name": [], "homework": [], "quizzes": [], "tests": []}


Hopefully you can see what I was trying to do there (and build on it! :smile: )

Since the first two lines of my code contain everything we need (student names in student_list, and dictionary statistics fields for each student in student_stats); is there a nicer way to make the 3 required dictionaries (rather than simply type them all out individually for each student as we had to in the exercise)??

  • if there is a nicer way, using only skills acquired so far, perhaps this exercise could be modified along those lines...

Thanks for any help / advice!


Hey mrsteel,

There isn't a way to modify the dictionaries using the material already taught, though there is a great discussion on stackoverflow.com that talks about adding keys to dictionaries. In short, they use a .update rather than .add like lists. That said, with two input lists (student names and items that each student dictionary should have) I think you could build this with a quick loop.

I'll have to test that if I get a chance.


Thanks for the reply :smile:

However, I feel that we should be able to do this already!..
Here is a closer attempt (I'm almost there (I think!)) - all I need to do now is make a new dictionary for each person, named after them (and not keep overwriting the template student_stats dictionary..)

student_list = ["lloyd", "alice", "tyler"]
student_stats = {"name": [], "homework": [], "quizzes": [], "tests": []}

i = 0
for student in student_list:
    student_stats["name"] = student_list[i]
    i += 1
    rename = student + "_stats" 
    print rename
    print student_stats

I think I'm getting close... any help to finish this off will be much appreciated!


Sorry, I should mention the desired result is the same as above:


Not so fast mrsteel! You're closer, but only while inside the for loop.

Things look great if you print in the for loop, but once you're out of it try printing those freshly minted dictionaries. You'll find them all empty "{}"! I'm guessing that's because you're not returning anything, so when the for loop iteration ends the data is lost.

not sure what the fix is yet, but clearly there is one.


lloyd = {
"name": "Lloyd",
"homework": [],
"quizzes": [],
"tests": []
alice = {
"name": "Alice",
"homework": [],
"quizzes": [],
"tests": []
tyler = {
"name": "Tyler",
"homework": [],
"quizzes": [],
"tests": []

run this code.
it will surely run wothiut any error


beeraider; Thanks for posting your working code. However I am seeking an improved way to answer this question (I've already passed it using the requested /standard method, but felt compelled to have a go doing it with some sort of improved loop method...which is probably just outside my grasp at the current moment!)

codyroche: any suggestions?


You're 100% correct, it will work perfectly. Unfortunately, you're manually typing the dictionaries like the later lessons do. mrsteel and I were discussing dynamic creation of the dictionaries using the lists as input. It's well beyond what's been taught so far, though it seems possible with the tools provided.


Still thinking on it. I'll take a look later. The code you've got just needs to return the dictionaries to work, but there might be another way. I've been seeing if I can work back from dynamic creation using a looped zip (cheating based on your criteria :grinning: ). Once that can loop successfully I think I can rebuild it using the tools at hand.


Cheers! Please do let me know if you do figure it out (if not: no worries) - I'm stuck on it for now but I'm sure I'll learn a better way to approach it soon enough anyway (hopefully).


try this code it worked for me:
lloyd = {"name": "Lloyd", "homework": [], "quizzes": [], "tests": []}
alice = {"name": "Alice", "homework": [], "quizzes": [], "tests": []}
tyler = {"name": "Tyler", "homework": [], "quizzes": [], "tests": []}