1 of 25

Section 6

Sets + Tuples + Itemgetter + Sorting

2 of 25

Logistics

  • HW4 Part 2 Due Monday (Nov. 11)
  • Programming and Written Check-In 6 Due Today (Nov. 9)

3 of 25

Set Review

4 of 25

Sets!

  • Sets are a type of data structure which is unordered and unindexed
  • There can be no duplicates
  • Imagine it as a bag of values

You can imagine a set that contains the values 1 through 6 like this:

1

2

3

4

5

6

5 of 25

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)

6 of 25

Add, Remove, Discard

Say we have the set s that has elements 1, 2, 3, 4 inside

  • Add
    • adds an element to the set
    • s.add(5)

1

3

2

4

5

  • Remove
    • takes out an existing element from the set (Must exist in the set!)
    • s.remove(5)
  • Discard
    • takes out an element from the set (doesn’t need to be in there already)
    • s.discard(5)
  • Pop
    • Returns a random element
    • s.pop()
    • Could return 1, 2, 3, or 4

7 of 25

What can be added into a set?

  • Although you can convert any data structure into a set, you can only add immutable types into a set (just like dict keys)
  • Data types that can not go in a set (mutable types)
    • Dictionaries
    • Lists
    • other sets
  • Data types that can go in a set (immutable types)
    • Integers
    • Floats
    • Booleans (but why would you do this?)
    • Strings
    • Tuples

Python

Python

TypeError: unhashable type: 'set'

Does a set of all sets contain itself?

8 of 25

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)

9 of 25

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

10 of 25

Set Operations

A | B

A & B

A - B

A ^ B

elements only in A

elements only in B

items in both

11 of 25

Example

Say we have two sets:

pets = {dog”, “hamster”, “lizard”, “cat}

felines = {cat”, “puma”, “lion”, “tiger}

dog

hamster

lizard

cat

lion

tiger

puma

cat

12 of 25

Example

Say we have two sets:

pets = {dog”, “hamster”, “lizard”, “cat}

felines = {cat”, “puma”, “lion”, “tiger}

dog

hamster

lizard

lion

tiger

puma

cat

13 of 25

Set Operations

  • pets | felines
    • {“dog”, “hamster”, “lizard”, “cat”, “puma”, “lion”, “tiger”}

  • pets & felines
    • {“cat”}

  • pets - felines
    • {“dog”, “hamster”, “lizard”}

  • pets ^ felines
    • {“dog”, “hamster”, “lizard”, “puma”, “lion”, “tiger”}

dog

hamster

lizard

lion

tiger

puma

cat

14 of 25

Tuples

15 of 25

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)

🚫

16 of 25

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!

17 of 25

Itemgetter

18 of 25

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

19 of 25

Sorting with Itemgetter in General

  • Useful for when you have a list of tuples where every item inside those tuples corresponds to a particular feature
    • ex: the element at index 0 of every tuple is a name, the element at index 1 of every tuple is an age)
    • lst = [('Anne', 5), ('Bob', 6), ('Carl', 3), ('Elisa', 2), ('Diana', 2)]

  • If you want to sort with the feature in the tuple at the index i, do so like this:
    • sorted_lst = sorted(lst, key = itemgetter(i))

  • Sort by the “least important” feature first (ie a tie breaker), the more important features last

20 of 25

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)

21 of 25

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)

22 of 25

Why do we care about itemgetter?

  • Very easy way to sort values on multiple attributes
  • Way to sort dictionaries into a list
    • if my_dict is a dictionary, then list(my_dict.items()) will return a list of key value tuple pairs!
  • Super important for hw5!

23 of 25

Kahoot

We’ll use this kahoot to go over problems 1, 2, and 4

No pressure to win (as always)

24 of 25

Section Problems

We’ll emphasize problems 5 and 6 (since they are the most relevant for the homework!)

Starter Code (Problem 5)

25 of 25

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

'''