FAQ: Sets - Set Intersection

This community-built FAQ covers the “Set Intersection” exercise from the lesson “Sets”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Learn Intermediate Python 3

FAQs on the exercise Set Intersection

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!
You can also find further discussion and get answers to your questions over in Language Help.

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head to Language Help and Tips and Resources. If you are wanting feedback or inspiration for a project, check out Projects.

Looking for motivation to keep learning? Join our wider discussions in Community

Learn more about how to use this guide.

Found a bug? Report it online, or post in Bug Reporting

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

This is another solution for the 2nd checkpoint. It’s quite lengthy
for song, tag in song_data.items():
if len(set(tags_int) & set(tag)) != 0 and song not in user_recent_songs:
recommended_songs[song] = tag

for tag in tags_int: for key, values in song_data.items(): if tag in values and key not in user_recent_songs.keys(): recommended_songs[key] = values

This loop adds songs to recommended_songs if they have EITHER of the two tags, if anyone is interested. Test it out by adding:
‘Just The Synth’: [‘synth’, ‘and’, ‘some’, ‘other’, ‘tags’], ‘Just Electronic’: [‘electronic’, ‘and’, ‘some’, ‘other’, ‘tags’]
To the song_data dictionary.

I used a dictionary comprehension as below;

recommended_songs = {song : tags for (song, tags) in song_data.items() if set(tags) & tags_int if song not in user_recent_songs}

This takes advantage of the boolean property of a non-empty set (in this case the intersection of set(tags) and tags_int) returning True.

5 Likes

Not the most efficient way but I ended up using nested for loops.

for song in song_data: print("printing", song) for tag in song_data[song]: print("printing tag", tag) if tag in tags_int: print("printing tag in tags_int", tag) if song not in user_recent_songs: recommended_songs[song] = tag

^^ still cleared step 2. But when I took a look at the solution and ran that code I got a different answer when I printed out the recommended songs.

:clap: Very concise! I didn’t remember two ‘if’ statements can be used in a row. Nice, thanks.

This is an awesome solution! I was a bit puzzled at first because nowhere in the course mentioned that empty sets are falsy in a Boolean context. Great find!

I came with the almost exact same solution.

I tend to write more lines while learning so that I see every single step.

my solution is:

for key, val in song_data.items():
    val_int = tags_int & set(val)
    if len(val_int) > 0 and key not in user_recent_songs:
        recommended_songs[key] = val