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:
Explain the difference between a set and a list.
Explain the difference between a dictionary and a list.
Predict the output of functions with sets and dictionaries.
Use .get to prevent key errors in dictionaries.
Process Objectives:
Predict the output of functions with sets and dictionaries.
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.
Strings
-
Yes, Strings are immutable.
Lists
-
No, Lists can change.
Tuples
-
Yes, Tuples are immutable.
Integers
-
Yes, Integers are immutable.
Q-4: Which of the following types can be added to a set?
Note
Sets can only store immutable items, even though sets themselves are mutable (can change).
5.45.3. Sets Do Not Allow Duplicates¶
Nothing will print. You will get an error since you are trying to add a duplicate item to a set.
-
No, it will actually ignore the duplicate item.
1, 2, 3
-
Yes, it ignores the duplicate item.
1, 2, 3, 1
-
No, sets do not allow duplicate items.
Q-5: What values will be printed last when the code below is run?
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
andother_set
.set1.intersection(other_set) - returns a new set with just the items that are in both
set1
andother_set
.set1.difference(other_set) - returns a new set with the items in
set1
that are not inother_set
.set1.issubset(other_set) - returns True if
set1
is a subset (has some or all of the same elements and no other elements) ofother_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
orother_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.
-
Q-14: Drag each symbol to the method it is equivalent to.
Read this page and try again.
- &
- intersection
- <=
- issubset
- >=
- issuperset
- -
- difference
- ^
- symmetric_difference
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.
-
Q-23: Drag each object to its type.
Read this page and try again.
- ["a", "b"]
- List
- ("a", "b")
- Tuple
- "ab"
- String
- {1, 2, 3}
- Set
- {"a": 5, "b": 2}
- Dictionary
Strings
-
Yes, Strings are immutable.
Lists
-
No, Lists can change.
Tuples
-
Yes, Tuples are immutable.
Sets
-
No, sets can change.
Dictionaries
-
No, Dictionaries can change.
Q-24: Which of the following types are immutable (don’t change)?
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.