Set
• Sets are used to store multiple items in a single variable. It store
collections of data.
• Sets are represented with curly brackets.
• Set items are unordered and do not allow duplicate values.
• Unordered means that the items in a set do not have a defined order.
thisset = {"apple", "banana", "cherry"}
print(thisset)
Duplicates Not Allowed
thisset = {"apple", "banana", "cherry", "apple"}
print(thisset)
Output
{'banana', 'cherry', 'apple'}
True and 1 is considered the same value
thisset = {"apple", "banana", "cherry", True, 1, 2}
print(thisset)
Output
{True, 2, 'banana', 'cherry', 'apple'}
thisset = {"apple", "banana", "cherry", False, True, 0}
print(thisset)
Output
{False, True, 'cherry', 'apple', 'banana'}
Length of a Set
• To determine number of items in a set is find by using len() function.
thisset = {"apple", "banana", "cherry"}
print(len(thisset))
Output
3
• A set can contain different data types:
set1 = {"abc", 34, True, 40, "male"}
Access the items
We cannot access items in a set by referring to an index or a key.
thisset = {"apple", "banana", "cherry"}
for x in thisset:
print(x)
Output
cherry
apple
banana
Add the items in set
• To add one item to a set use add() method.
thisset = {"apple", "banana", "cherry"}
thisset.add("orange")
print(thisset)
Output
{'cherry', 'apple', 'banana', 'orange'}
• To add items from another set into the current set, use the update()
method.
x = {"apple", "banana", "cherry"}
y= {"pineapple", "mango", "papaya"}
x.update(y)
print(x)
Output
{'apple', 'mango', 'cherry', 'pineapple', 'banana', 'papaya'}
Remove Item
• To remove an item in a set, use the remove(), or the discard() method.
y = {"apple", "banana", "cherry"}
y.remove("banana")
print(y)
Output
{'apple', 'cherry'}
thisset = {"apple", "banana", "cherry"}
thisset.discard("banana")
print(thisset)
Output
{'apple', 'cherry'}
• the pop() method to remove an item, but this method will remove a
random item, so you cannot be sure what item that gets removed.
• The return value of the pop() method is the removed item.
Pop method
thisset = {"apple", "banana", "cherry"}
x = thisset.pop()
print(x) #removed item
print(thisset) #the set after removal
Output
cherry
{'banana', 'apple'}
• The clear() method empties the set
thisset = {"apple", "banana", "cherry"}
thisset.clear()
print(thisset)
The del keyword will delete the set
thisset = {"apple", "banana", "cherry"}
del thisset
Union
set1 = {"a", "b", "c"}
set2 = {1, 2, 3}
set3 = set1.union(set2)
print(set3)
Output
{3, 'b', 2, 'c', 'a', 1}
• the | operator instead of the union() method, and you will get the
same result.
set1 = {"a", "b", "c"}
set2 = {1, 2, 3}
set3 = set1 | set2
print(set3)
Output
{1, 'a', 3, 'c', 'b', 2}
• Join multiple sets with the union() method
set1 = {"a", "b", "c"}
set2 = {1, 2, 3}
set3 = {"John", "Elena"}
set4 = {"apple", "bananas", "cherry"}
myset = set1.union(set2, set3, set4)
print(myset)
Output: {'a', banana, 1, John, 3, 'c', cherry, 2, apple, Elena, 'b'}
intersection
set1 = {"apple", "banana", "cherry"}
set2 = {"google", "microsoft", "apple"}
set3 = set1.intersection(set2)
print(set3)
Output
{'apple'}
• use the & operator instead of the intersection() method, and you will
get the same result.
set1 = {"apple", "banana", "cherry"}
set2 = {"google", "microsoft", "apple"}
set3 = set1 & set2
print(set3)
Output
{'apple'}
Difference
• The difference() method will return a new set that will contain only
the items from the first set that are not present in the other set.
set1 = {"apple", "banana", "cherry"}
set2 = {"google", "microsoft", "apple"}
set3 = set1.difference(set2)
print(set3)
Output
{'banana', 'cherry'}
Symmetric Differences
• The symmetric_difference() method will keep only the elements that
are NOT present in both sets. (apart from common things)
set1 = {"apple", "banana", "cherry"}
set2 = {"google", "microsoft", "apple"}
set3 = set1.symmetric_difference(set2)
print(set3)
Output
{'google', 'banana', 'microsoft', 'cherry'}
set1 = {"apple", "banana", "cherry"}
set2 = {"google", "microsoft", "apple"}
set3 = set1 ^ set2
print(set3)