Learn

In Python, set and frozenset items cannot be accessed by a specific index. This is due to the fact that both containers are unordered and have no indices. However, like most other Python containers, we can use the in keyword to test if an element is in a set or frozenset.

Here are some examples of finding if elements exist in a set and frozenset:

# Given a list of song tags song_tags = {'guitar', 'acoustic', 'folk', 'country', 'live', 'blues'} # Print the result of testing whether 'country' is in the set of tags or not print('country' in song_tags)

Would output:

True

This also works for frozenset:

song_tags = {'guitar', 'acoustic', 'folk', 'country', 'live', 'blues'} frozen_tags = frozenset(song_tags) print('rock' in frozen_tags)

Would output:

False

Let’s use the in keyword to work with more user tags in our music application!

Instructions

1.

Now that we have learned about the using the in keyword for set containers, we have decided to update the tagging system by automatically removing unrelated tags.

Our team members are working on a CSV file containing all allowed keywords, but for now, we will be using a list of allowed words when programming your logic.

To start, store the tag data from song_data_users into a set called tag_set.

2.

Next, we want to capture all of the tags in tag_set that don’t belong.

Create a list called bad_tags. Then, iterate through each element of tag_set, adding tags to bad_tags that don’t belong.

3.

Now, let’s remove all the incorrect tags from tag_set.

Using the collected bad_tags, write another loop to iterate over each of the tags in bad_tags, and remove the elements from tag_set so we have only the allowed tags.

4.

Finally, replace the value of the key, 'Retro Words' inside of song_data_users so that it is equal to the updated tag set.

Print song_data_users to see the result.

Sign up to start coding

By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.
Already have an account?