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 trialagzagi
2,399 PointsCan anyone help me understand argument.intersection(COURSES[course])
Dear fellow students,
after solving both parts of this challenge there is this syntax, which I do not understand, or rather do not understand why it is necessary.
In both functions you find an if-statement, followed by
'''a.intersection(COURSES[course])''' and
'''a.issubset(COURSES[course])''' respectively.
What I do not quite understand is why using the following code is not enough:
'''a.intersection(course)''' instead of using '''a.intersection(COURSES[course])'''
I obviously am making some mistake in my thought process.
I would be very thankful for every insight to help me figure this out.
Best regards
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(a):
lst = []
for course in COURSES:
if a.intersection(COURSES[course]):
lst.append(course)
return lst
def covers_all(a):
lst = []
for course in COURSES:
if a.issubset(COURSES[course]):
lst.append(course)
return lst
2 Answers
Jason Anello
Courses Plus Student 94,610 PointsHi Agustin,
With the way you have your for loop set up, it's going to iterate over the keys in the COURSES dictionary.
The names of the courses are the keys. So if you do a.intersection(course)
then you'll have something like a.intersection("Python Basics")
But what you really need to pass in is the set of topics that "Python Basics" covers.
COURSES[course]
will give you the set of topics associated with whatever the course happens to be.
Alternatively, you could use the items()
method on dictionaries and set your for loop up like this:
for course, topics in COURSES.items():
if a.intersection(topics):
The items() method will give you back both the key and the value of each dictionary item. Then you could simply pass the set of topics into the intersection method. You won't have to access the dictionary with a particular key like you did before.
agzagi
2,399 PointsHello Jason,
thank you very much for your reply!!!
I thought the explanation would be what you have very clearly explained in your reply. But I also thought that if I pass a key in, the loop would go through the key's value as well.
So it turns out it is not enough to tell the loop to go into the dictionary and iterate key over key, but I have to tell the loop to go into the dictionary, iterate over every key and, while doing so, iterate also through te value of each key.
Thank you very much once again for taking the time to reply in such a clear and understandable way!
Best regards