5.45. Group Work: Functions with Sets and Dictionaries

It is best to use a POGIL approach with the following. In POGIL students work in groups on activities and each member has an assigned role. For more information see https://cspogil.org/Home.

Note

If you work in a group, have only one member of the group fill in the answers on this page. You will be able to share your answers with the group at the bottom of the page.

Learning Objectives

Students will know and be able to do the following.

Content Objectives:

Process Objectives:

5.45.1. What is a Set?

A set is like a list in that holds items, but it does not hold items in an particular order. You can add an item to a set and remove an item from a set, just like you can do with a list. You can also check if an item is in a set.

Run this code to see what it prints.

5.45.2. Sets only Contain Immutable Items

Run this code to see what happens. Then remove any code that causes and error and run it again.

Note

Sets can only store immutable items, even though sets themselves are mutable (can change).

5.45.3. Sets Do Not Allow Duplicates

Before you keep reading...

Runestone Academy can only continue if we get support from individuals like you. As a student you are well aware of the high cost of textbooks. Our mission is to provide great books to you for free, but we ask that you consider a $10 donation, more if you can or less if $10 is a burden.

Run this code to see what it prints.

Note

Sets only store unique items. If you try to add a duplicate item to a set it will be ignored.

5.45.4. Adding Mutliple Items to a Set

Run this code to see what it prints. You will get an error. Remove the code that causes the error and run it again.

Note

The set add method only adds one item to a set. To add several items use the set update method.

5.45.5. Set Methods

You can use the following methods with sets.

  • set1.union(other_set) - returns a new set with all the items from set1 and other_set.

  • set1.intersection(other_set) - returns a new set with just the items that are in both set1 and other_set.

  • set1.difference(other_set) - returns a new set with the items in set1 that are not in other_set.

  • set1.issubset(other_set) - returns True if set1 is a subset (has some or all of the same elements and no other elements) of other_set.

  • set1.issuperset(other_set) - returns True if set1 is a superset (has all the same elments and may have other elements) of other_set.

  • set1.semmantic_difference(other_set) - returns a new set with the items that are in either set1 or other_set, but not both. This is also known as an exclusive or (XOR).

Run this code to see what it prints.

Run this code to see what it prints.

Run this code to see what it prints.

5.45.6. Creating Sets

You can innitialize a set with a string, list, or tuple. The set will only contain the unique items.

Run this code to see what it prints.

5.45.7. Sorting Sets

Run this code to see what it prints.

Note

The function sorted(set) will return a new list sorted in ascending order.

Drag the blocks from the left and put them in the correct order on the right to define a function unique_characters(strings) that takes a list of strings and returns a sorted list of all the distinct characters in the strings.

5.45.8. Dictionaries

A dictionary stores a value for a key. The keys must be immutable and unique.

Run this code to see what it prints.

Q-20: Look at the Python code below. What do you think will happen when you run the following code?

Run this code to see what it prints.

There is another way to update the value for a key that works even if the key isn’t in the dictionary already.

Run this code to see what it prints.

Note

The better way to increment a count at a key is to use dict[key] = dict.get(key,0) + 1. This will avoid a key error if the key isn’t in the dictionary and the code is shorter.

If you worked in a group, you can copy the answers from this page to the other group members. Select the group members below and click the button to share the answers.

The Submit Group button will submit the answer for each each question on this page for each member of your group. It also logs you as the official group submitter.

You have attempted 1 of 26 activities on this page