Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialAnders Axelsen
3,471 PointsTas 1/2: How do i iterate through the dictionary properly?
Hi there!
I don't know, if the dict.COURSES
is unecessary. I would like to know the 'mechanics' of iterating. I don't know, what I am targeting. Is there a course video, specifically adressing this? #if you have an idea, I'm all ears.
Thanks in advance
COURSES = {
"Python Basics": {"Python", "functions", "variables",
"booleans", "integers", "floats",
"arrays", "strings", "exceptions",
"conditions", "input", "loops"},
"Java Basics": {"Java", "strings", "variables",
"input", "exceptions", "integers",
"booleans", "loops"},
"PHP Basics": {"PHP", "variables", "conditions",
"integers", "floats", "strings",
"booleans", "HTML"},
"Ruby Basics": {"Ruby", "strings", "floats",
"integers", "conditions",
"functions", "input"}
}
def covers(key):
returnable = []
for key in dict.COURSES():
if "basics" in key:
returnable.append(key)
return returnable
Anders Axelsen
3,471 PointsHi Frank Genova.
I am trying to follow your advice. I get, however, a syntax error.
This is what I put in the script in workspace:
COURSES = {
"Python Basics": {"Python", "functions", "variables",
"booleans", "integers", "floats",
"arrays", "strings", "exceptions",
"conditions", "input", "loops"},
"Java Basics": {"Java", "strings", "variables",
"input", "exceptions", "integers",
"booleans", "loops"},
"PHP Basics": {"PHP", "variables", "conditions",
"integers", "floats", "strings",
"booleans", "HTML"},
"Ruby Basics": {"Ruby", "strings", "floats",
"integers", "conditions",
"functions", "input"}
def covers(param):
returnable = []
for course in COURSES:
if COURSES[course] & param:
returnable.append(course)
return returnable
print(returnable)
frankgenova
Python Web Development Techdegree Student 15,616 Pointsif you want to print() what was returned then you should move print() to the edge and you would call covers(param) from within the print().
also 'and' does not equal '&' for sets for sets '&' implies intersection
intersection(*others) set & other Return a new set with elements common to the set and all others.
https://docs.python.org/3/library/stdtypes.html?highlight=set#set
As it stands I do not think it will hit your print because you have it indented within the function.
def covers(param):
# your code
return returnable
print(covers({"Python"}) # note it is not in covers() function
Anders Axelsen
3,471 PointsThe &
made perfect sense. It's about intersection!
For the printing, though, I think I did something out of syntax. I get an error on line 17, which is def covers(param):
def covers(param):
returnable = []
for course in COURSES:
if COURSES[course] & param:
returnable.append(course)
return returnable
print(covers({"Python"}))
I also tried: print covers({"Python"})
1 Answer
Christopher Shaw
Python Web Development Techdegree Graduate 58,248 PointsYou need to iterate the COURSES to get the course names (keys).
You then test each value set againsted the set supplied, for an intersection (common). If there is an intersection, add to the new list.
Another issue you have is that you used the variable key twice. You overrode the inital setting with the for loop.
def covers(param):
returnable = []
for course in COURSES:
if COURSES[course] & param:
returnable.append(course)
return returnable
Anders Axelsen
3,471 PointsThank you for clearing it up, Christopher.
It suprised me - I had to use &
and not and
. Do you know if there is a particular reason for this?
frankgenova
Python Web Development Techdegree Student 15,616 Pointsfrankgenova
Python Web Development Techdegree Student 15,616 PointsI did not use dict because course is already an iterable dictionary comprised of the key (courses such as 'Python Basics' with values (a list of topics). I agree with creating an empty list ('returnable') as a first step. What I did is ran it on the side with some extra print statements to see what is being iterated. for example if you simply use for key in COURSES and print key on each loop you get an idea of what you are looking at. Then considering your key (which could be a set of topics) see if it has the right set relationship to what you are iterating on. Consider the set operators to get the best choice.