Python 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.
Sets
We know that we can keep data in a list format.
What might be the problem with these two examples?
In the first list, we have repetition in unique_colors
section, making them not unique! The second example is more of a use case. If you wanted to keep track of subscribers by email, len(subscribed_emails)
wouldn't give you an accurate number because of the duplicates.
Enter the Set
Notice the {}
versus the []
.
A list is a collection of elements, contained within square brackets []
.
However, there is a specific version of a list called a set. What makes a set different is that all of the elements in a set must be unique. That is to say, nothing can appear more than once in a set. Sets are defined with curly braces {}
.
A Set is an unordered collection data type that is iterable, mutable, and has no duplicate elements. Python’s set class represents the mathematical notion of a set.
Making a Set
We can make a set directly as above, declaring it and placing the values in curly brackets rather than square brackets.
The other way to create a set is by using set()
with a pre-existing list.
Instead of passing a list into set()
, we could just type a list directly in:
When making a set via a list Python removes duplicates automatically.
Why Sets?
The major advantage of using a set, as opposed to a list, is that it has a highly optimized method for checking whether a specific element is contained in the set
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.
Test it out
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 namedclothing_set
.Use a
for
loop to print out bothclothing_list
andclothing_set
.Try to print an index!
Answers ```python clothing_list = ["red", "black", "brown", "black", "purple"] clothing_set = set(clothing_list) for item in clothing_list: print(item) for item in clothing_set: print(item) for i in range(len(clothing_set)): print(clothing_set[i]) # => TypeError: 'set' object does not support indexing ```
Adding to a Set
How do we add more to a set?
Why is it append()
in lists but add()
in sets? It's 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?
Removing from 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 begreen
,red
, oryellow
.
The same way, we need to be careful about removal:
Tuples
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? What if we wanted that data to be immutable?
We don't want:
We want rainbow_colors
to be immutable - the list cannot be changed.
How we do that in Python?
Enter the Tuple
Sets are one specific type of list.
No duplicates
Mutable.
Uses
{}
Tuples are another specific type of list.
Duplicates
Immutable.
A list that cannot be changed.
Uses
()
Going back to the rainbow list we defined earlier. We want to make that a tuple:
Why Tuples?
Tuples are useful when you need data protection through immutability—when you never want to change the list. You can still access them through indexes (because the data never changes, including the position!)
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!).
Test it out
Let's declare a tuple named
seasons
and set it to have the valuesfall
,winter
,spring
, andsummer
.We'll print the tuple and each value.
Then we'll try to reassign them.
Answers
```python seasons = ("fall", "winter", "spring", "summer") print(seasons) for season in seasons: print(season) seasons[0] = "autumn" # => TypeError: 'tuple' object does not support item assignment ```
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()
andremove(element)
Tuple:
Has duplicates, but immutable: You can't change it!
("red", "red", "yellow", "green")
will always be("red", "red", "yellow", "green")
.
Types
Variables certainly can hold a lot of different types of data! We've just been learning that sets, tuples, and lists are similar, but easily confused. How do we tell?
type()
tells us what a variable is: set, tuple, list, dictionary, integer, string - anything!
Additional Reading
Last updated