A Set is an unordered collection of unique and immutable objects. Sets can be heterogenous in composition. A set is it's own category of data structures, as is a sequence and a mapping. However, the set category in Python has one data structure, literally known as the set, while sequences in Python are strings, lists, tuples, etc. In this lecture, we will cover:
1. Initializing a Set
> Curly bracket notation
2. Access and Modifying a Set
3. Initialize a Set using set() function
We can initialize a set using curly bracket notation or the set() built-in function. In sets, all objects must be hashable types, like the key of a dictionary. This means that lists and dictionaries cannot be in a set, but objects like numbers and strings can be.
In [18]:
# Creating a Set using curly bracket notation
my_set = {"MONDAY", "TUESDAY", "WEDNESDAY"}
Sets can be heterogenous, as long as they are unique, too.
In [24]:
my_set = {"Hello", 9.0, "foo", 19}
Notice how all of the objects in the set we made are unique. What if we make a set like this?
In [29]:
# A set with non-unique elements?
my_set = {"Hello", "Hello", 9.0, 9.0}
Fear not - no errors or exceptions are raised when adding items that already exist in a set, they are just considerd by the set once. While the cell does not accurately represent this, we can print and verify.
In [28]:
# A set with non-unique elements?
my_set = {"Hello", "Hello", 9.0, 9.0}
# Verifying that the set only considered each item once
print(my_set)
The only built-in set function we need to use is discussed in the next section of this notebook. Let's take a look at set methods before we talk about the set() method.
1. add()
2. clear()
3. copy()
4. difference()
5. discard()
6. pop()
7. remove()
Let's begin by initializing a set with curly bracket notation, and then using these methods.
In [6]:
# Create a Set of strings
yoz_set = {"Temple Run", "2048", "Fruit Ninja", "Crossy Road"}
The add() method adds an item to a set (mutator).
In [8]:
# Add an object to our set
yoz_set.add("Blocky")
# Verify
print(yoz_set)
A call to clear() on a set deletes all of its objects.
In [11]:
# Clear a Set
yoz_set.clear()
# Verify
print(yoz_set)
The copy() method returns a copy of the set object. It allows the programmer to quickly replicate the object distinctly (not object reference).
In [13]:
# Recreate yoz_set
yoz_set = {"Temple Run", "2048", "Fruit Ninja", "Crossy Road"}
# Copy and save the set
my_second_yoz_set = yoz_set.copy()
# Verify
print(my_second_yoz_set)
The difference() method returns the objects that make a set different from the implicit parameter.
implicit_set.difference(explicit_set)
Returns objects from the implicit_set that do not exist in the explicit set.
In [23]:
# Create a set
my_set = {"woo", "foo"}
# Create another set
my_2_set = {"rsoos", "dja"}
# Difference
my_set.difference(my_2_set)
Out[23]:
The discard(object) method removes an object from the set. If object is not in set, nothing is removed.
In [34]:
# Create a Set
yoz_set = {"Temple Run", "2048", "Fruit Ninja", "Crossy Road"}
# Discard "Fruit Ninja"
yoz_set.discard("Fruit Ninja")
# Verify that "Fruit Ninja" is gone from set
print(yoz_set)
The pop() removes and returns an arbitrary element from the set.
In [36]:
# Create a Set
my_set = {"woo", "foo"}
# Pop an element
my_set.pop()
# verify something was removed
print(my_set)
The remove() method removes an object from a set. However, unlike the discard() method, it raises a KeyError if the element is not present.
In [37]:
# Create a Set
my_set = {"Chicago", "Tahalasee", "Houston"}
# Remove Houston from the set
my_set.remove("Houston")
# Verify Houston is gone
print(my_set)
In [38]:
# Try to remove Boston from the set (it doesn't exist)
my_set.remove("Boston")
We can also use the set() built-in function to create a set. Set has two constructors:
set() - Creates an empty set object
set(iterable) - Creates a set from an iterable
If we create an empty set using set(), we use the built-in set methods to operate on it.
In [39]:
# Create a set with set()
x = set()
In [41]:
# Adding items to the set
x.add("Hello")
x.add(1)
x.add("River")
# Verify set() and add() work
print(x)