1. What is a Python set ?
A Python set is a modifiable collection of distinct hashable objects, such as lists and tuples, etc. It is an unordered collection of objects, which means that it does not save the position of the elements or the order of insertion and therefore cannot access the elements using indexes.
A Python set is a Python implementation of the notion of a set in mathematics. A set object has appropriate methods to perform mathematical set operations like union, intersection, difference, etc.
A python set object contains one or more items, not necessarily of the same type, which are separated by a comma ',' and surrounded by braces {}.
Example (creating a set)
1 2 3 |
mySet = {"pants", "shirt", "shoes"} print(mySet) # Displays: {'pants', 'shirt', 'shoes'} |
Remark
Python sets are unordered, and therefore each time you run the code, the items will appear in random order.
2. Accessing Elements of a Python Set
You can't access elements of a set by referencing an index, because sets are unordered, elements don't have indexes. But you can loop through the elements of the set using a for loop or query if a specified value is present in a set using the in keyword.
Example: displaying elements of a set
1 2 3 4 5 6 7 8 9 |
mySet = {"Java", "Python", "C++"} for x in mySet: print(x) """ Which displays: Java Python C++ """ |
Note that each time you rerun the program, there is a different display order. Which means that the order in the Python sets doesn't matter!
Example: Checking if an element belongs to a set
To check if an element exists within a python set, you can simply use the 'in' keyword:
1 |
element in python_set |
1 2 3 |
mySet = {"Pen", "Pencil", "Eraser"} print("Pencil" in mySet) # displays: True print("Notebook" in mySet) # displays: False |
3. Length or cardinality of a Python set
To know the length (cardinal) of a Python set, we use the len() method.
Example: length of a python set
1 2 3 4 |
mySet = {"Pen", "Pencil", "Eraser"} cardinal = len(mySet) print("card(mySet) = ", cardinal) # prints card(mySet) = 3 |
4. Operations on a Python set
4.1 Add one or more elements to a Python set
To add an element to a Python set, we use the add() method:
Example: adding an element to a set
1 2 3 |
mySet = {"Pen", "Pencil", "Eraser"} mySet.add("Notebook") print(mySet) # displays: {'Eraser', 'Pencil', 'Pen', 'Notebook'} |
Remark
We can also add several elements at the same time by using the update() method:
Example: add several elements
1 2 3 4 |
mySet = {"Pen", "Pencil", "Eraser"} mySet.update(["Notebook", "Binder", "Pencil Case"]) print(mySet) # displays: {'Pen', 'Pencil', 'Pencil', 'Notebook', 'Binder', 'Eraser'} |
4.2 Remove an element from a Python set
To remove an element from a Python set, you have two choices: the remove() method or the discard() method.
Example: remove "Pencil" by the remove() method
1 2 3 |
mySet = {"Pen", "Pencil", "Eraser"} mySet.remove("Pencil") print(mySet) # displays {'Eraser', 'Pen'} |
Remark
If the element to be removed does not exist, remove() will generate an error.
Example: remove "Pencil" by discard() method
1 2 3 |
mySet = {"Pen", "Pencil", "Eraser"} mySet.discard("Pencil") print(mySet) # displays {'Eraser', 'Pen'} |
Remark
Unlike the remove() method, the discard() method does not generate any error when the item to be removed does not exist! The delete statement will simply be ignored!
Remark
you can also use the pop() method to delete an element, but this method will delete the last element. Remember that the sets are unordered and you won't know which element will be deleted. The deletion is totally random!
4.3 Dumping a Python set
To clear Python set, we use the clear() method:
Example empty a Python set
1 2 3 |
mySet = {"Pen", "Pencil", "Eraser"} mySet.clear() print(mySet) # output: set{} which means an empty set |
4.4 Delete a set
To delete a Python set, use the command del:
Example: Delete a set
1 2 3 4 |
mySet = {"Pen", "Pencil", "Eraser"} del mySet print(mySet) # displays the error message: builtins.NameError: name 'mySet' is not defined |
5. Summary of methods associated with a Python set
- add(): add an element to the set
- clear(): removes all 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 elements from this set that are also included in another specified set
- discard(): remove the specified item
- intersection(): returns a set, which is the intersection of two other sets.
- intersection_update(): removes elements from this set that are not present in other specified sets.
- isdisjoint(): indicates whether two sets have an intersection or not.
- issubset(): indicates if another game contains this game or not.
- issuperset(): indicates whether this set contains another set or not.
- pop(): remove an item 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 of this set and another
- union(): returns a set containing the union of the sets
- update(): updates the set with the union of this set and others
Younes Derfoufi
CRMEF OUJDA