Python Programming: Sets and Tuples


Learning Objectives

After this lesson, you will be able to:

  • Perform common actions with sets.
  • Perform common actions with tuples.
  • Know when to use different data structures.

Discussion: Lists

Here are some lists:

What could be a problem here?


Introducing Sets

Lists:

Sets: Lists without duplicates!

  • Notice the [] versus the {}.

How Can We Make a Set?

Making a set via a list - Python removes duplicates automatically.

Making a set directly, in curly braces:


Important Note: Sets

Lists are always in the same order:

  • my_list = ["green", "yellow", "red"] is always going to be["green", "yellow", "red"]
  • my_list[0] is always "green"; my_list[1] is always "yellow"; my_list[2] is always "red".

Sets are not! Like dictionaries, they’re in any order.

  • my_set = {"green", "yellow", "red"} could later be {"red", "yellow", "green"}!
  • my_set[0] could be "green", "red", or "yellow" - we don’t know!

We cannot do: print(my_set[0]) - it could be anything! Python won’t let us.


We Do: Creating a Set from a List

Let’s pull up a new set_practice.py file and make some sets!

  • Make a list clothing_list containing the main color of your classmates’ clothing.
  • Using clothing_list, make a set named clothing_set.
  • Use a for loop to print out both clothing_list and clothing_set.
  • Try to print an index!

We Do: Adding to a Set

How do we add more to a set?

add vs append - this is because we can’t guarantee it’s going at the end!

Let’s a few colors to clothing_list and clothing_set, then print them.

  • What happens if you add a duplicate?

We Do: Removing from a List and a Set

Remember, lists are always the same order: ["green", "yellow", "red"].

  • my_list[0] is always “green”.

Remember, sets are not!

  • With the set {"green", "yellow", "red"}, my_set[0] could be green, red, or yellow.

The same way, we need to be careful about removal:


Quick Review: Sets vs. Lists

Lists:

  • The original, normal object.
  • Created with [].
  • append(), insert(index), pop(), pop(index).
  • Duplicates and mutable.

Sets:

  • Lists without duplicates.
  • Created with {} or with set(my_list).
  • add() and remove(element).

Quick Review: Sets vs. Lists


Discussion: Immutability Thoughts

A set is a type of list which doesn’t allow duplicates.

What if, instead, we have a list we don’t want to change?

We don’t want:

We want rainbow_colors to be immutable - the list cannot be changed.

How we do that in Python?


Introducing: Tuples

Sets are one specific type of list.

  • No duplicates, but mutable.

Tuples are another specific type of list.

  • Duplicates, but immutable.
  • A list that cannot be changed.

When should you use a tuple?

  • When you need data protection through immutability.
  • When you never want to change the list.

Tuple Syntax

  • Created with parentheses ().
  • Access values via indices (like regular lists, but not like sets).
  • Tuples can be printed with a for loop (just like a set or list!).

We Do: Tuples

Let’s declare a tuple named seasons and set it to have the values fall, winter, spring, and summer. We’ll print the tuple and each value. Then we’ll try to reassign them (we can’t)!


Quick Review: Sets, Tuples, Lists

List:

  • The original, normal object: ["red", "red", "yellow", "green"].
  • Has duplicates; mutable: append(), insert(index), pop(), pop(index)

Set:

  • List without duplicates: {"red", "yellow", "green"}.
  • Mutable: add() and remove(element)

Tuple:

  • Has duplicates, but immutable: You can’t change it!
  • ("red", "red", "yellow", "green") will always be ("red", "red", "yellow", "green").

Quick Review: Sets, Tuples, Lists


Introducing Types

Variables certainly can hold a lot!

  • Sets, tuples, and lists are easily confused.
  • type() tells us what a variable is: set, tuple, list, dictionary, integer, string - anything!

Try it:


You Do: List Types Practice

Create a local file, sets_tuples.py. In it:

  • Create a list ([]), set ({}), and tuple (()) of some of your favorite foods.
  • Create a second set from the list.

Next, in every list type that you can:

  • Add "pizza" anywhere; append "eggs" to the end.
  • Remove "pizza".
  • Re-assign the element at index 1 to be "popcorn".
  • Remove the element at index 2 and re-insert it at index 0.
  • Print the element at index 0.

Print your final lists using a loop, then print their types. Don’t throw an error!


Summary and Q&A

We’ve learned two new types of lists:

Sets:

  • A mutable list without duplicates.
  • Handy for storing emails, usernames, and other unique elements.

Tuples:

  • An immutable list that allows duplicates.
  • Handy for storing anything that won’t change.

Additional Reading