A set is a collection which is unordered, unchangeable*, and unindexed. Sets do not allow duplicate values.
*Note: Set items are unchangeable, but you can remove items and add new items.
1. Create set
Sets are written with curly brackets {}.
thisset = {"apple", "banana", "cherry"}
print(thisset)
Because sets are unordered, you cannot be sure in which order the items will appear.
No Duplicates Allowed
If you try to create a set with duplicate items, the duplicates will be ignored:
thisset = {"apple", "banana", "cherry", "apple"}
print(thisset) # {'banana', 'cherry', 'apple'}
Note: The values True and 1 are considered the same value in sets, and are treated as duplicates. False and 0 are also considered the same.
2. Add items
To add one item to a set use the add() method.
thisset = {"apple", "banana", "cherry"}
thisset.add("orange")
print(thisset)
To add items from another set into the current set, use the update() method.
thisset = {"apple", "banana", "cherry"}
tropical = {"pineapple", "mango", "papaya"}
thisset.update(tropical)
The object in the update() method does not have to be a set, it can be any iterable object (tuples, lists, dictionaries etc.).
3. Remove items
To remove an item in a set, use the remove(), or the discard() method.
remove(): If the item to remove does not exist,remove()will raise an error.discard(): If the item to remove does not exist,discard()will NOT raise an error.
thisset = {"apple", "banana", "cherry"}
thisset.remove("banana")
thisset.discard("apple")
You can also use the pop() method, but because sets are unordered, pop() will remove a random item. The return value of the pop() method is the removed item.
x = thisset.pop() # Removes a random item
thisset.clear() # Empties the set
4. Loop through set
You can loop through the set items by using a for loop.
thisset = {"apple", "banana", "cherry"}
for x in thisset:
print(x)
5. Join sets
There are several ways to join two or more sets in Python.
-
union()andupdate(): Both will combine sets and exclude any duplicate items.set1 = {"a", "b" , "c"} set2 = {1, 2, 3} set3 = set1.union(set2) # Returns a new set -
intersection()orintersection_update(): Keep ONLY the duplicates (items that exist in both sets).set1 = {"apple", "banana", "cherry"} set2 = {"google", "microsoft", "apple"} set3 = set1.intersection(set2) # {'apple'} -
symmetric_difference(): Keep all items EXCEPT the duplicates.set1 = {"apple", "banana", "cherry"} set2 = {"google", "microsoft", "apple"} set3 = set1.symmetric_difference(set2) # {'banana', 'cherry', 'google', 'microsoft'}
6. Set methods
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 |
discard() | Remove the specified item |
issubset() | Returns whether another set contains this set or not |
issuperset() | Returns whether this set contains another set or not |
Discussion
Loading comments...