Print all Keys with a single "print" command


#1



Hello, I was wondering what the code would be for printing all the keys in the dictionary, each in a separate line, without having to type multiple lines of code.

https://www.codecademy.com/courses/python-beginner-en-qzsCL/0/4?curriculum_id=4f89dab3d788890003000096


I was thinking something like:

print students["name"] + \nstudents["homework"] + \nstudents["quizzes"] + \nstudents["tests"]

or even

print students ["name", \n"homework", \n"quizzes", \n"tests"]

but neither seem to work; I'm not sure if the syntax is horribly wrong of the the exercise is not allowing me to enter a different code than expected.

Appreciate all your help,
Cheers :slight_smile:


Replace this line with your code.


#2

you are going to need a for loop, since you need to access each student:

for student in students:

but if the content of your loop is a single line, you can put it on the same line as the for loop:

for student in students: print student

here:

print students["name"] + \nstudents["homework"] + \nstudents["quizzes"] + \nstudents["tests"]

you are pretty close, but normally when you have a string:

print "hello world\n"

\n is part of the string, so the new line character should be in quotation marks:

x="hello"
y="world"
print x + "\n" + y

#3

Based on your suggestion, I tried the following code for every student in the students list:

print tyler["name"] + "\n"tyler["homework"] + "\n"tyler["quizzes"] + "\n"tyler["tests"]

but I must be doing something wrong because it is not working

Here is my code:
lloyd = {
"name": "Lloyd",
"homework": [90.0, 97.0, 75.0, 92.0],
"quizzes": [88.0, 40.0, 94.0],
"tests": [75.0, 90.0]
}
alice = {
"name": "Alice",
"homework": [100.0, 92.0, 98.0, 100.0],
"quizzes": [82.0, 83.0, 91.0],
"tests": [89.0, 97.0]
}
tyler = {
"name": "Tyler",
"homework": [0.0, 87.0, 75.0, 22.0],
"quizzes": [0.0, 75.0, 78.0],
"tests": [100.0, 100.0]
}
students = [lloyd, alice, tyler]

for lloyd in students:
print lloyd["name"]
print lloyd["homework"]
print lloyd["quizzes"]
print lloyd["tests"]

for alice in students:
print alice["name"]
print alice["homework"]
print alice["quizzes"]
print alice["tests"]

for tyler in students:
print tyler["name"]
print tyler["homework"]
print tyler["quizzes"]
print tyler["tests"]

This works but somehow it prints every student 3 times


#4

Not sure if this is what your looking for but as the instructions first say, "for each student in your students list, print out that student's data, as follows:"
Funny how instructions can give you big hints without even trying! It tells you that your for statement should be for student in students: try making one just like the instructions suggest! with print student's name print student's homework etc. etc. instead of making three for each student. For since your list has all three students in it just using one will make everything about all three students appear on the console log.


#5

Well I'm sorry for not knowing, just trying to learn here.

I thought we had to somehow print the info by using the dictionaries within the list as opposed to doing it directly from the dictionaries; hence my confusion.

Anyhow, all sorted now tyvm :slight_smile:


#6

Nay listen your fine! That is why the forums are here is for people to learn/see the light and I know I have overlooked stuff and banged my head on the wall when after hours of trying I finally learned what I was doing wrong so we are just trying to save you from some headaches!...and advil :stuck_out_tongue:


#7

did you figure out you forgot the + after "\n"? It was in the code of my answer


#8

Thx for pointing it out again, I did add the +"\n" but also did in fact miss the + after.


#9

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.