1. Define a Python Set
- A Python set is an unordered collection of objects, unlike sequences like lists and tuples in which each element is indexed.
- A Python set cannot contain duplicates: you can only find elements in it zero or once. All members of a set must be hashable, like dictionary keys.
- For example, scalars such as integers, floats, tuples, and strings are hashable; however dictionaries, lists, and sets are not.
- A Python sets are written with braces {....}.
Creating a set:
mySet = {"notebook" , "book" , "pencil" , "pen" , "eraser"} print(mySet) # output: {"notebook" , "book" , "pencil" , "pen" , "eraser"}
Note
Sets are not ordered, so items will appear in random order.
2. Access to the elements of a Python set
Unlike lists, you cannot access elements of a set by referencing an index, because sets are not ordered, items do not have indexes. But you can browse the elements of the set using a for loop or ask if a specified value is present in a set using the in keyword.
Example: displaying elements of a set
mySet = {"Pen", "Pencil", "Eraser"} for x in mySet: print(x) """ output: Pencil Pen Eraser """
You notice that each time you make a new run of the program, there is a different display order. Which means that the order in the Python sets does not matter!
Example: checking membership of an element
mySet = {"Pen", "Pencil", "Eraser"} print("Pencil" in mySet) # displays: True print("Notebook" in mySet) # displays: False
3. Length or Cardinal of a Python Set
To know the length(cardinal) of a Python set, we use the len() method
Example: length of a python set
mySet = {"Pen", "Pencil", "Eraser"} cardinal = len(mySet) print("card(mySet) =", cardinal) # displays card(mySet) = 3
4. Operations: add, delete or update a Python set
4.1 Add one or more elements to a Python set
- To add an element to a Python set, you can use the add() method:
Example: adding an element to a set
mySet = {"Pen", "Pencil", "Eraser"} mySet.add("Notebook") print(mySet)
- We can also add several elements at the same time with the update() method:
Example: Add multiple items
mySet = {"Pen", "Pencil", "Eraser"} mySet.update(["Notebook", "Schoolbag", "Kit"]) print(mySet)
4.2 Remove an element from a Python set
To remove an element from a Python set, you have two choices for the remove() method or the discard() method.
Example: remove "Pencil" by the remove() method
mySet = {"Pen", "Pencil", "Eraser"} mySet.remove( "Pen") print(mySet) # show {'Eraser', 'Pencil'}
Note:
If the item to delete does not exist, remove() will generate an error.
Example: delete "Pencil" by the discard() method:
mySet = {"Pen", "Pencil", "Eraser"} mySet.discard( "Pen") print(mySet) # show {'Eraser', 'Pencil'}
Note:
Unlike the remove() method, the discard() method does not generate an error when the item to delete does not exist! The delete instruction will simply be ignored!
NOTE:
You can also use the pop() method to delete an item, but this method will delete the last item. Remember that sets are not ordered and you will not know which item will be deleted. The deletion is totally random!
4.3 Empty a Python Set
- To empty Python together, use the clear() method
Example: empty a Python set
mySet = {"Pen", "Pencil", "Eraser"} mySet.clear() print(mySet) # displays set {} which means an empty set
4.4 Delete a set
To delete a Python set, you can use the del command
Example: delete a set
mySet = {"Pen", "Pencil", "Eraser"} mySet print(mySet) # displays the error message: builtins.NameError: name 'mySet' is not defined
5. Main 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 items from a set that are also included in another specified set
- discard(): delete the specified item
- intersection(): returns a set, which is the intersection of two other sets.
- intersection_update(): removes items in this set that are not present in other specified sets.
- isdisjoint(): Indicates whether two sets intersect or not.
- issubset(): indicates whether another game contains this game or not.
- issuperset(): indicates whether this set contains another set or not.
- pop(): removes an element from the set
- remove(): delete 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 sets
- update(): update the set with the union of this set and others
Younes Derfoufi
my-courses.net