A set is Python’s collection which is unindexed and unordered. A set is written as a sequence of comma-separated values between {} curly brackets. Like tuple, the set is also immutable means you are not allowed to change the set elements. A set doesn’t contain any duplicate items.
Set can also be used to calculate the mathematical set operations such as union, intersection, difference, symmetric difference, etc.
Create a Set
color_set = {"green", "red", "blue", "blue"} print(color_set)
# Output {'blue', 'green', 'red'} # It discard the duplicate elements.
# set is unordered, so it is not necessary the items will appear in order as they are inserted.
Access Items
Python does not allow you to access the set item by index, because the set is unordered and the items have no index.
But, you can loop through the set to print the items of the set.
color_set = {"green", "red", "blue"} for e in color_set: print(e)
# Output blue red green
You can also use Python’s membership operator in & not in with set to check whether the item exists in the set or not.
color_set = {"green", "red", "blue"} if 'yellow' in color_set: print("color exist in set") else: print("color doesn't exist in set")
# Output color doesn't exist in set
Update Items
Once the set is created, Python doesn’t allow you to update the items of the set, because you need the index of an item to change it. But, the items of the set have no index.
Add Items
- set.add(item) – method used to add the element to set.
color_set = {"green", "red", "blue"} color_set.add("yellow") print(color_set)
# Output {'red', 'blue', 'green', 'yellow'}
- set.update(list) – function used to add the multiple elements to the set.
color_set = {"green", "red", "blue"} color_set.update(["yellow","black"]) print(color_set)
# Output {'blue', 'black', 'red', 'yellow', 'green'}
Remove Item
1. set.remove(item) – Method used to remove the given item. If the given item does not exist in the set, Python interpreter raises an error.
color_set = {"green", "red", "blue","yellow"} color_set.remove("red") # Remove "red" from the set print(color_set) # Output {'blue', 'yellow', 'green'}
color_set = {"green", "red", "blue","yellow"} color_set.remove("black") # Raise an error, as "black" is not present in set print(color_set) # Output KeyError: 'black'
2. set.discard(item) – Method used to remove the given item. But, the discard() function will not raise an error, even if the given item is not present in the set.
color_set = {"green", "red", "blue","yellow"} color_set.discard("black") # Won't raise an error print(color_set) # Output {'green', 'yellow', 'blue', 'red'}
3. set.pop() – Method used to remove and return the last item of the set. But, you do not know which items get removed, because you are totally unaware of the position of the set items.
color_set = {"green", "red", "blue","yellow"} color_set.pop() print(color_set) # Output {'blue', 'yellow', 'red'}
4. set.clear() – Method used to empties the set.
color_set = {"green", "red", "blue","yellow"} color_set.clear() print(color_set) # Output set() # Empty set
The union() method
The union() method used to return the new set with the unique items from both sets. It will exclude duplicate items.
Example 1 –
set1 = {"green", "red", "blue"} set2 = {1,2,3} union_set = set1.union(set2) print(union_set) # Output {1, 2, 3, 'green', 'red', 'blue'}
Example 2 –
set1 = {"green", "red", "blue"} set2 = {1,2,3,"blue"} union_set = set1.union(set2) # It will exclude duplicate items print(union_set) # Output {1, 'blue', 'green', 'red', 2, 3}
The intersection() method
The intersection() method used to create a new set with the common items from both sets.
set1 = {"green", "red", "blue"} set2 = {1,2,3,"blue"} set3 = set1.intersection(set2) print(set3) # Output {'blue'}
The difference() method
The difference() method used to return the set with items which only exist in set1 and not exist in set2.
set1 = {"green", "red", "blue"} set2 = {1,2,3,"blue"} set3 = set1.difference(set2) print(set3) # Output {'green', 'red'}
The symmetric_difference() Method
The symmetric_difference method used to return a set with all items from both list, except the items which are presents in both sets.
set1 = {"green", "red", "blue"} set2 = {1,2,3,"blue"} set3 = set1.symmetric_difference(set2) print(set3) # Output {1, 2, 3, 'red', 'green'}
. . .