Python Sets

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)

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

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:

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




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

Remark

We can also add several elements at the same time by using the update() method:

Example: add several elements

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

Remark

If the element to be removed does not exist, remove() will generate an error.

Example: remove "Pencil" by discard() method

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

4.4 Delete a set

To delete a Python set, use the command del:

Example: Delete a set

5. Summary of methods associated with a Python set

  1. add(): add an element to the set
  2. clear(): removes all elements from the set
  3. copy(): returns a copy of the set
  4. difference(): returns a set containing the difference between two or more sets.
  5. difference_update(): removes elements from this set that are also included in another specified set
  6. discard(): remove the specified item
  7. intersection(): returns a set, which is the intersection of two other sets.
  8. intersection_update(): removes elements from this set that are not present in other specified sets.
  9. isdisjoint(): indicates whether two sets have an intersection or not.
  10. issubset(): indicates if another game contains this game or not.
  11. issuperset(): indicates whether this set contains another set or not.
  12. pop(): remove an item from the set
  13. remove(): removes the specified element
  14. symmetric_difference(): returns a set with the symmetric differences of two sets
  15. symmetric_difference_update(): inserts the symmetric differences of this set and another
  16. union(): returns a set containing the union of the sets
  17. update(): updates the set with the union of this set and others

 

Younes Derfoufi
CRMEF OUJDA

Leave a Reply