diff --git a/native-datatypes.html b/native-datatypes.html index 50a2b35..901ec49 100644 --- a/native-datatypes.html +++ b/native-datatypes.html @@ -442,7 +442,11 @@ IndexError: pop from empty list
FIXME +
A set is an unordered “bag” of unique values. A single set can contain values of any datatype. Once you have a two sets, you can do standard set operations like union, intersection, and set difference. + +
First things first. Creating a set is easy.
>>> a_set = {1} ①
@@ -454,12 +458,12 @@ IndexError: pop from empty list
>>> a_set
{1, 2}
{}).
+You can create a set out of a list. +
You can also create a set out of a list.
>>> a_list = ['a', 'b', 'mpilgrim', True, False, 42] @@ -469,12 +473,12 @@ IndexError: pop from empty list>>> a_list ③ ['a', 'b', 'mpilgrim', True, False, 42]
set() function. (Pedants who know about how sets are implemented will point out that this is not really calling a function, but instantiating a class. I promise you will learn the difference later in this book. For now, just know that set() acts like a function, and it returns a set.)
+You can create an empty set. +
Don’t have any values yet? Not a problem. You can create an empty set.
>>> a_set = set() ①
@@ -485,38 +489,37 @@ IndexError: pop from empty list
>>> len(a_set) ④
0
>>> not_sure = {} ⑤
->>> type(not_sure) ⑥
+>>> type(not_sure)
<class 'dict'>
set() with no arguments.
+{}, perhaps? That would denote an empty dictionary, not an empty set. (You’ll learn about dictionaries later in this chapter.)
+FIXME +
There are two different ways to add values to an existing set: the add() method, and the update() method.
>>> a_set = {1, 2}
->>> a_set.add(3) ①
+>>> a_set.add(4) ①
>>> a_set
-{1, 2, 3}
+{1, 2, 4}
>>> len(a_set) ②
3
>>> a_set.add(1) ③
>>> a_set
-{1, 2, 3}
+{1, 2, 4}
>>> len(a_set) ④
3
add() method takes a single argument, which can be any datatype, and adds the given value to the set.
+@@ -524,23 +527,24 @@ IndexError: pop from empty list>>> a_set {1, 2, 3} >>> a_set.update({2, 4, 6}) ① ->>> a_set +>>> a_set ② {1, 2, 3, 4, 6} ->>> a_set.update({3, 6, 9}, {1, 2, 3, 5, 8, 13}) ② +>>> a_set.update({3, 6, 9}, {1, 2, 3, 5, 8, 13}) ③ >>> a_set {1, 2, 3, 4, 5, 6, 8, 9, 13} ->>> a_set.update([10, 20, 30]) ③ +>>> a_set.update([10, 20, 30]) ④ >>> a_set {1, 2, 3, 4, 5, 6, 8, 9, 10, 13, 20, 30}
update() method takes one argument, a set, and adds all its members to the original set. It’s as if you called the add() method with each member of the set.
+update() method with any number of arguments. When called with two sets, the update() method adds all the members of each set to the original set (dropping duplicates).
+update() method can take a number of different datatypes, including lists. When called with a list, the update() method adds all the items of the list to the original set.
FIXME +
There are three ways to remove individual values from a set. The first two, discard() and remove(), have one subtle difference.
>>> a_set = {1, 3, 6, 10, 15, 21, 28, 36, 45}
@@ -560,24 +564,35 @@ IndexError: pop from empty list
File "<stdin>", line 1, in <module>
KeyError: 21
discard() method takes a single value as an argument and removes that value from the set.
+discard() method with a value that doesn’t exist in the set, it does nothing. No error; it’s just a no-op.
+remove() method also takes a single value as an argument, and it also removes that value from the set.
+remove() method raises a KeyError exception.
Like lists, sets have a pop() method.
+
>>> a_set = {1, 3, 6, 10, 15, 21, 28, 36, 45}
->>> a_set.pop()
+>>> a_set.pop() ①
1
>>> a_set.pop()
3
>>> a_set.pop()
36
>>> a_set
-{6, 10, 45, 15, 21, 28}
+{6, 10, 45, 15, 21, 28}
+>>> a_set.clear() ②
+>>> a_set
+set()
+>>> a_set.pop() ③
+Traceback (most recent call last):
+ File "<stdin>", line 1, in <module>
+KeyError: 'pop from an empty set'
pop() method removes a single value from a set a returns the value. However, since sets are unordered, there is no “last” value in a set, so there is no way to control which value gets removed. It is essentially random.
+clear() method removes all values from a set, leaving you with an empty set. This is equivalent to a_set = set(), which would create a new empty set and overwrite the previous value of the a_set variable.
+KeyError exception.