# Set Data Type
Python has a data type for sets. Like a list, a set is a collection of items. However sets are unordered and with no duplicates.
Basic usages of a set include membership testing and removing duplicate items. Sets also support mathematical operations like union, intersection, difference, and symmetric difference.
# Set
We also use a pair of {}
to create a set.
But unlike dictionary where we store key/value pairs, we only put values in it.
basket = {'apple', 'orange', 'apple', 'pear', 'orange', 'banana'}
print(basket)
2
what about empty sets?
We can use the set()
function to create an empty set, because {}
represents an empty dictionary.
empty_set = set()
print(empty_set)
2
# set comprehension
It's just like list comprehension.
print({fruit.upper() for fruit in basket})
print({fruit.capitalize() for fruit in basket if fruit != "banana"})
# operators for set
For example, we have two sets:
basket1 = {"apple", "banana", "coconut", "dragon fruit"}
basket2 = {"apple", "pear", "watermelon"}
2
-
would return items in the left hand set but not in the right hand set,
print(basket1 - basket2)
print(basket2 - basket1)
2
|
would return all items from both sets (union).
print(basket1 | basket2)
&
would return items that appear in both sets (intersection)
print(basket1 & basket2)
^
would return items that appear in only one set.
print(basket1 ^ basket2)
We can also use in
to check if an item exists in a set.
print("apple" in basket1)
print("banana" not in basket2)
2
# functions for set
We use add()
to add an item to the set.
basket1.add("peach")
print(basket1)
basket1.add("apple")
print(basket1)
2
3
4
5
We use update()
to add a collections of items to the list.
basket1.update({"peach", "honey melon"})
print(basket1)
basket1.update(["peach", "honey melon"])
print(basket1)
2
3
4
5
We use intersection()
to get items that appear in both sets, just like the &
operator.
print(basket1.intersection(basket2))
print(basket2.intersection(basket1))
2
We use union()
to get all items from both sets, just like the |
operator.
print(basket1.union(basket2))
print(basket2.union(basket1))
2
We use symmetric_difference()
to get items that appear in only one set, just like the ^
operator.
print(basket1.symmetric_difference(basket2))
print(basket2.symmetric_difference(basket1))
2
We use difference()
to get te items that only appear in itself but not the other set, just like the -
operator.
print(basket1.difference(basket2))
print(basket2.difference(basket1))
2
We use issuperset()
/ issubset()
to check if it's the super / sub set of another set.
print(basket2.issuperset(basket1))
print(basket2.issubset(basket1))
print(basket1.issuperset({"apple"}))
print(basket2.issubset({"apple", "pear", "watermelon", "mango"}))
2
3
4
5
We use pop()
to remove a random item from a set.
basket2.pop()
print(basket2)
2
We use discard()
to remove a given item from a set.
basket1.discard("apple")
print(basket1)
basket1.discard("shoe")
print(basket1)
2
3
4
5
We use clear()
to remove all items from a set.
basket1.clear()
print(basket1)
2