Sets In Python
Python Sets: Comprehensive Theory
Sets are used to store multiple items in a single variable. A set is a collection which is unordered, unchangeable*, and unindexed.
1. Characteristics of Sets
- Unordered: Sets do not have a defined order. Items can appear in a different order every time you use them, and they cannot be referred to by index or key.
- Unchangeable: Set items are unchangeable, meaning we cannot change the items after the set has been created.
- Note: Although you cannot change items, you can remove items and add new items.
- Unindexed: You cannot access items in a set by referring to an index.
- No Duplicates: Sets cannot have two items with the same value. If a duplicate value is added, it is ignored.
2. Creating a Set
Sets are written with curly brackets {}.
myset = {"apple", "banana", "cherry"}
The set() Constructor
You can also use the set() constructor to create a set.
thisset = set(("apple", "banana", "cherry")) # note the double round-brackets
3. Accessing Set Items
Since sets are unindexed, you cannot access items by index. However, you can loop through the set items using a for loop, or ask if a specified value is present by using the in keyword.
thisset = {"apple", "banana", "cherry"}
for x in thisset:
print(x)
print("banana" in thisset) # Output: True
4. Adding & Removing Items
Adding Items
- To add one item to a set use the
add()method. - To add items from another set (or any iterable) into the current set, use the
update()method.
thisset = {"apple", "banana"}
thisset.add("cherry")
# Adding multiple items
tropical = {"pineapple", "mango", "papaya"}
thisset.update(tropical)
Removing Items
remove(): Removes the specified element. Raises an error if the item does not exist.discard(): Removes the specified element. Does NOT raise an error if the item does not exist.pop(): Removes a random item (since sets are unordered, you don't know which item will be removed).clear(): Empties the set.
5. Join Sets (Operations)
Python has several ways to join two or more sets:
| Operation | Method | Description |
| :--- | :--- | :--- |
| Union | union() or | | Returns a new set with all items from both sets. |
| Intersection | intersection() or & | Returns a new set with only items present in both sets. |
| Difference | difference() or - | Returns a new set with items in the first set but NOT in the second. |
| Symmetric Difference | symmetric_difference() or ^ | Returns a set with items that are in either set, but NOT in both. |
6. Set Methods Reference Table
| 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 |
| intersection() | Returns a set, that is the intersection of two other sets |
| isdisjoint() | Returns whether two sets have a intersection or not |
| issubset() | Returns whether another set contains this set or not |
| issuperset() | Returns whether this set contains another set or not |
| pop() | Removes an element from the set |
| remove() | Removes the specified element |
| union() | Return a set containing the union of sets |
| update() | Update the set with the union of this set and others |