Complex Dictionary Creation


#1
students = ["lloyd" , "alice" , "tyler"]
keys = ["name", "homework", "quizzes", "tests"]
def student_file(a):
    student_file = {each: {key: [] for key in keys}for each in a}
    for each in a:
        student_file[each]["name"] = each.capitalize()
    return student_file
#creates function that forms a dictionary for each student

print student_file(students)

I am trying to make this more complicated than necessary just to increase my knowledge.

Basically, I was trying to write a function that would produce the three dictionaries that we need. I attempted to use:

for each in a:
    return student_file[each]

as my return statement at the end, but this ultimately only produced one dictionary for Lloyd.


#2

My temporary work-around:

keys = ["name", "homework", "quizzes", "tests"]
def student_file(a):
    file = {key: [] for key in keys}
    file["name"] = a.capitalize()
    #replaces blank key pair for "name" with students' Name
    return file
    return  #formatting line
#creates a dictionary holding a dictionary for each student

lloyd =  student_file("lloyd")
alice = student_file("alice")
tyler = student_file("tyler")
#assigns the dictionary created for each student to the student's name

#3

And of course I went through all of that and the next lesson removes your code entirely to input their own data and code. Grrrr.


#4

Damn, man! I've just tried to find out what I missed up during my learning course while I've tried to understand your code! Then I've checked out what's codecademy expected code solution is and promptly I realized you coded way to progressie for me! My brain is broken here...

Sorry for bad english. Broken brain, remember!
Dude, you could write out that you are a master of coding!)))) Who knew that your soulution includes parts what I didn't explored yet.

Still thanks for something new! Cheers! Hags and tags (dont know what does it mean, but sometihng funny I guess).


#5

@datapro55798

Python is the third language that I've used Codecademy for (the other two being at least two years ago). When I sat down to learn it, I realized that the other two languages have barely stuck in my head at all. Both times I was just doing the coding to get the results that they wanted, and that's it. With Python, I decided that I would solve each problem to get the correct answer and then comment and complicate.

Basically, I add comments to my code using # so that I can quickly look back at my logic. After that, I put """ before and after my code to comment out the multiple lines of code that gets my answer so that the site will stop telling me that I am right and try to get me to move on. Once that is done, I try to create a solution for universal problems of the same sort (which often requires going to external websites to help figure it out).

So for this, I discovered one-line for loops (which you will learn about way later on from Codecademy). But my goal was to make it so that you could adapt your student list to be any students and have it create dictionaries for them.

I've found that doing this helps to learn more, faster and helps you to retain what you have already learned. I sat down last night to do more work on here after taking two weeks or so off and had no trouble getting back into the swing of things.

Good luck coding!


#6

Good for you. Wish you luck!


#7

Hey @tbhesswebber ,

Yes, going beyond the requirements of the exercises is a great way to deepen your understanding. Codecademy will sometimes discard what you have done in the exercises that follow, but you will learn a lot, regardless of that, and in fact looking for workarounds may help you learn even more.

There are many "hidden" Python courses on Codecademy that you may want to try out. Some of them have flaws, but working around the flaws, in order to pass and ultimately get a badge, can be a learning experience. One example is Letter Frequency.