Sets are used to store multiple items in a single variable
Sets are one of 4 built-in data types in Python used to store collections of data
Sets are written with curly brackets { }
A set is an unordered, immutable, unindexed and do not allow duplicate values
myset = {"red", "green", "blue"} print(myset) #Output: {'blue', 'green', 'red'} |
myset = {"red", "green", "blue","red", "green"} #remove duplicate value print(myset) #Output: {"red", "green", "blue"} |
Unordered means that the items in a set do not have a defined order so it's unindexed which means can't access an item in the set by the index
myset = {"red", "green", "blue"} print(myset) #Output: {'blue', 'green', 'red'} |
In the above code snippet, the given order is "red", "green" & "blue" but the output order is 'blue', 'green' & 'red' that's why we say the set is unordered
Every set element is unique (no duplicates) and must be immutable (cannot be changed).
thisset = {"one", "two", "three"} thisset[1] = "four" print(thisset) #Output: TypeError: 'set' object does not support item assignment |
However, a set itself is mutable. We can add or remove items from it.
thisset = {"one", "two", "three"} thisset.add("four") print(thisset) #Output: {'four', 'two', 'one', 'three'} thisset.update({"five", "six"}) print(thisset) #Output: {'two', 'six', 'five', 'four', 'one', 'three'} |
Python has a set of built-in methods that you can use on sets.
Method | Description |
---|---|
add() | adds an element to the set |
clear() | removes all the elements from the set |
copy() | returns a copy of the set |
difference() | returns a set containing the difference between two or more sets |
difference_update() | removes all the elements of another set from this set |
discard() | remove an element from a set if it is a member |
intersection() | returns a set, that is the intersection of two other sets |
intersection_update() | removes the items in this set that are not present in other, specified set(s) |
isdisjoint() | returns boolen value whether two sets have a intersection elements or not |
issubset() | returns boolean value whether another set contains this set or not |
issuperset() | returns boolean value whether this set contains another set or not |
pop() | removes an element from the set |
remove() | removes the specified element |
symmetric_difference() | returns a set with the symmetric differences of two sets |
symmetric_difference_update() | inserts the symmetric differences from this set and another |
union() | return a set containing the union of sets |
update() | update the set with the union of this set and others |
NOTE: We are not going to see all the methods as some of them are similar to the list methods.
Venn Diagram
If you have ever seen a Venn diagram in your school.
For Example, two circles are overlapping each other and you may find a union and intersection value between two or three given sets that are similar to these methods.
returns a set containing the difference between two or more sets
my_set = {1,2,3,4,5,6} your_set = {4,5,6,7,8,9} result_set = my_set.difference(your_set) print(result_set) #Output: {1, 2, 3} |
remove an element from a set if it is a member (no-return)
my_set = {1,2,3,4,5,6} my_set.discard(5) print(my_set) #Output: {1, 2, 3, 4, 6} my_set.discard(10) print(my_set) #Output: {1, 2, 3, 4, 6} |
removes all the elements of another set from this set
my_set = {1,2,3,4,5,6} your_set = {4,5,6,7,8,9} my_set.difference_update(your_set) print(my_set) #Output: {1, 2, 3} |
removes all the elements of another set from this set
difference() and difference_update() methods do the same thing but difference returns the difference and difference_update updates the set.
my_set = {1,2,3,4,5,6} your_set = {4,5,6,7,8,9} my_set.difference_update(your_set) print(my_set) #Output: {1, 2, 3} |
returns a set, that is the intersection of two other sets (common elements among the sets)
my_set = {1,2,3,4,5,6} your_set = {4,5,6,7,8,9} print(my_set.intersection(your_set)) #Output: {4, 5, 6} |
returns boolen value whether two sets have a intersection elements or not
my_set = {1,2,3,4,5,6} your_set = {4,5,6,7,8,9} print(my_set.isdisjoint(your_set)) #Output: False my_set = {1,2,3} your_set = {4,5,6,7,8,9} print(my_set.isdisjoint(your_set)) #Output: True |
return a set containing the union of sets
my_set = {1,2,3,4,5,6} your_set = {4,5,6,7,8,9} print(my_set.union(your_set)) #Output: {1, 2, 3, 4, 5, 6, 7, 8, 9} |
returns boolean value whether another set contains this set or not
my_set = {4,5,6} your_set = {4,5,6,7,8,9} print(my_set.issubset(your_set)) #Output: True my_set = {1,2,3} your_set = {4,5,6,7,8,9} print(my_set.issubset(your_set)) #Output: False |
returns boolean value whether this set contains another set or not
my_set = {4,5,6} your_set = {4,5,6,7,8,9} print(your_set.issuperset(my_set)) #Output: True my_set = {1,2,3} your_set = {4,5,6,7,8,9} print(your_set.issuperset(my_set)) #Output: False |
return a set of elements that are in either my_set or your_set, but not in their intersection
my_set = {1,2,3,4,5,6} your_set = {4,5,6,7,8,9} print(my_set.symmetric_difference(your_set)) #Output: {1, 2, 3, 7, 8, 9} |
If you have any doubts or queries related to this chapter, get them clarified from our Python Team experts on ibmmainframer Community!