Section 6
Sets + Tuples + Itemgetter + Sorting
Logistics
Set Review
Sets!
You can imagine a set that contains the values 1 through 6 like this:
1
2
3
4
5
6
All of the following code would make a set called s with the elements 1-4 inside
s = set([1, 2, 3, 4])
s = {1, 1, 1, 1, 1, 1, 2, 3, 4}
1
3
2
4
s = {1, 2, 3, 4}
s = set()
s.add(1)
s.add(2)
s.add(3)
s.add(4)
Add, Remove, Discard
Say we have the set s that has elements 1, 2, 3, 4 inside
1
3
2
4
5
What can be added into a set?
Python
Python
TypeError: unhashable type: 'set'
Does a set of all sets contain itself?
Looping through a Set
To see all elements in a set, we can loop through it
Would print 1, 2, 3, 4 in some random order
1
3
2
4
for element in s:
print(element)
Checking if Something is in a Set: In
We can use in to see if an element is in a set
Returns True
Returns False
1
3
2
4
2 in s
6 in s
Set Operations
A | B
A & B
A - B
A ^ B
elements only in A
elements only in B
items in both
Example
Say we have two sets:
pets = {“dog”, “hamster”, “lizard”, “cat”}
felines = {“cat”, “puma”, “lion”, “tiger”}
dog
hamster
lizard
cat
lion
tiger
puma
cat
Example
Say we have two sets:
pets = {“dog”, “hamster”, “lizard”, “cat”}
felines = {“cat”, “puma”, “lion”, “tiger”}
dog
hamster
lizard
lion
tiger
puma
cat
Set Operations
dog
hamster
lizard
lion
tiger
puma
cat
Tuples
Tuples
Tuple is a collection which is ordered and unchangeable.
Lists and tuples are similar, but have different properties
The table at the right shows what kind of things you can do with a tuple, but not a list.
Let the data structure be called name. A ✅ means you can do it, a 🚫 means it won’t work
Description | Example | list | tuple |
indexing | name[i] | ✅ | ✅ |
negative indexing | name[-i] | ✅ | ✅ |
slicing | name[i:j] | ✅ | ✅ |
checking if item exists | item in name | ✅ | ✅ |
looping | for item in name | ✅ | ✅ |
length | len(name) | ✅ | ✅ |
changing items | name[i] = item | ✅ | 🚫 |
appending items | name.append(item) | ✅ | 🚫 |
put in a set | set().add(name) | 🚫 | ✅ |
use a dict keys | dict(name:val) | 🚫 | ✅ |
Making a Tuple
These are all ways to make tuples:
t = (1, 2, 3)
t = tuple([1, 2, 3])
t = (1,)
Note that to make a one element tuple you need to add a comma after the one value! (1) would not work!
Itemgetter
Itemgetter
Itemgetter returns a function.
from operator import itemgetter
get_3rd_item = itemgetter(2)
get_3rd_item([7, 3, 8]) -> 8
# this is the same as
itemgetter(2)([7, 3, 8]) -> 8
Sorting with Itemgetter in General
Sorting with itemgetter
lst = [('Anne', 5), ('Bob', 6), ('Carl', 3), ('Elisa', 2), ('Diana', 2)]
# sort alphabetically by name
alphabetical_lst = sorted(lst, key = itemgetter(0))
# sort by the number (lowest number first)
sorted(lst, key = itemgetter(1))
# sort by the number (highest number first)
sorted(alphabetical_lst, key = itemgetter(1), reverse = True)
Sorting with itemgetter based on two criteria
lst = [('Anne', 5), ('Bob', 6), ('Carl', 3), ('Elisa', 2), ('Diana', 2)]
# sort by the number (highest number first), break ties alphabetically
# alphabetize first
sorted_lst = sorted(lst, key = itemgetter(0))
# then sort by number
sorted_lst = sorted(sorted_lst, key = itemgetter(1), reverse = True)
Why do we care about itemgetter?
Kahoot
We’ll use this kahoot to go over problems 1, 2, and 4
No pressure to win (as always)
Section Problems
We’ll emphasize problems 5 and 6 (since they are the most relevant for the homework!)
Starter Code (Problem 5)
Programming Check In
''
Return true if subset_one contains all elements of subset_two.
Return false if subset_two contains an extra element besides
the ones found in subset_one except if the extra element is 1.
If the only additional element in subset_two is 1, return True.
Arguments:
subset_one: a set of integers
subset_two: a set of integers
Returns: False if subset_two contains additional elements besides
the ones found in subset_one except if the extra element is 1
'''