Navbar menu

Sets in python

What is Set?
  • Set is an unordered collection data type with no duplicate elements. (unique elements)
  • Using curly braces {} or keyword set, we can create a set in Python.
  • In general, the set function is used to eliminate duplicate elements in a list.
Creating a Set
  • In python, the set can be created using curly braces {} or keyword set.
  • Ex: S = { 'y', 't', 'p', 'h', 'n', 'o'} or S = set('y', 't', 'p', 'h', 'n', 'o')
  • Even if we pass duplicate elements while creating a set. It won't throw an error instead it removes the duplicate and returns the set with unique elements.
Set with a unique element without duplicates
Accessing a Set
  • Set can be iterated but it is un-indexed. This means we can't perform index access since the set is unordered.
  • Ex: 
Set Iteration
  • Similarly, we can check whether an element present in the given set or not using 'in' keyword.
  • Ex:
Accessing set Element using 'in' keyword 
Set Methods
  1) Adding new element to the set - add()
  • Using add() method, we can add an element to the given set. Since the set is unordered, there is no particular index to the newly added element.
add() method in set
2) Removing element from the set - remove() / discard() / pop()

remove()

  • Using remove() method, we can remove a particular element in the given set.
remove() method
  • If the element isn't present in the set, then it will throw an error like below. 
Error is thrown by remove() method
discard()
  • Discard method also removes the element from the set. 
  • But the difference between remove() and discard is, discard() method will not throw an error if the element not present in the set.
discard() method in set
pop()
  • We can also use pop() method to remove an element from the set but pop() method always removes the last element
  • Since the set is unordered, so we will not know what element gets removed by pop()
  • Removed item will be shown by the return value of pop()
pop() method
3) Adding more than one element to the set - update()
  • We can add multiple items to a set using the update() method
Adding multiple-element using update() method
4) Clearing & deleting the set- Clear(), del
  • To empties, a set, clear() method is used.
Clear() method
  • Del keyword used to delete the set completely
Del method
5) Union of Sets - |
  • Union operation merges elements of two sets and creates a new set that contains all the distinct elements from both sets.
  • In the below example, "Apple"(element) is present in both the sets. But when executed, the result has only one "Apple".
Union operation in Set
6) Intersection of Sets - &
  • Intersection operation creates a new set that contains a common element from both sets.
  • In the below example, "Apple" is present both sets. When executed, the result contains only "Apple" which is common in both sets.
Intersection operation in Set
7) Difference of Sets - '-'
  • Difference operation creates a new set that contains only the element from set 1 and none from set 2.
  • In the below example, the difference operation was performed on two sets fruit1 and fruit2. When executed, the result contains only elements that are unique in the first set fruit1.
Difference of set operation
Set methods
  • In Python, we have a set of built-in methods that you can use on Sets
Method
Description
add()
Adds an element to the set
clear()
Removes all elements from the set
copy()
Returns a copy of the set
difference()
Returns the difference of two or more sets as a new set
difference_update()
Removes all elements of another set from this set
discard()
Removes an element from the set if it is a member. (Do nothing if the element is not in set)
intersection()
Returns the intersection of two sets as a new set
intersection_update()
Updates the set with the intersection of itself and another
isdisjoint()
Returns True if two sets have a null intersection
issubset()
Returns True if another set contains this set
issuperset()
Returns True if this set contains another set
pop()
Removes and returns an arbitrary set element. Raises KeyError if the set is empty
remove()
Removes an element from the set. If the element is not a member, raises a KeyError
symmetric_difference()
Returns the symmetric difference of two sets as a new set
symmetric_difference_update()
Updates a set with the symmetric difference of itself and another
union()
Returns the union of sets in a new set
update()
Updates the set with the union of itself and others

No comments:

Post a Comment