Skip to main content

Python 3 Part 6 - Understanding Set data structure

Understanding Set data structure

Table of content

  1. Set
  2. How to define set in python
  3. Accessing set elements
  4. Set methods

Set

Set is an unordered data-structure with no duplicates. One can define set using {} curly brackets.

How to define set in python


checking the type() and len() of set.

Accessing set elements

Trying to access data-elements using index throws an error.

checking dir on set object

Set Methods

1) set.add(x) : adds an element to set.


2) set.clear() : clears out the elements from the set.


3) set.copy() : returns deep copy of set object


4) set.difference(<set_object>) : returns the diff of two or more set objects.


5) set.difference_update(<set_obj>) : removes all the element from current object. (Minus operation)


6) set.discard(x) : remove pass element from set if it is member.

7) set.intersection(<set_obj>) : returns intersection of two set objects.


8) set.intersection_update(<set_obj>) : Update the current set object with intersection.


9) set.isdisjoint(<set_obj>) : Verify whether two set object has NULL intersection. If yes, then returns True else False


10) set.issubset(<set_obj>) : Verifies whether current set is subset object passed.


11) set.superset(<set_obj>) : Verifies whether current set object is superset  of passed object. If yes, then returns True else False.


12) set.pop() : removes and returns arbitrary element from set. Gives KeyError if passed value is not present in set.


13) set.remove(x) : remove passed value from set. If that value is not present in set gives KeyError.


14) set.symmetric_difference(<set_obj>) : returns symmetric_differenece of two set.


15) set.symmetric_difference_update(<set_obj>) : update the current set with symmetric differenece.


16) set.union(<set_obj>) : returns the union of two sets.


17) set.update(<set_obj>) : update the set with union between itself and passed set object.


Thank you folks. If you like this Python 3 post, please do checkout my other post on  Django with Python 3 series












Most viewed

Ruby on Rails Part 4 - Exception Handling

  Ruby on Rails Part 4 - Exception Handling  Table of content Exception Handling retry raise ensure else  catch and throw Exception classes Exception Handling Enclose the code that could raise an exception with a begin/end block and use rescue clauses to tell Ruby the types of exceptions that you want to handle. The syntax for exception handling : 
 begin  
      #- statements
 rescue OneTypeOfException       #-
 handle the exception rescue AnotherTypeOfException       #- 
handle the exception else       
# Other exceptions
 ensure 
      # ensure block is always executed
 end 
 Everything from begin to rescue is protected 

in the block. 
If an exception occurs during the execution of this block of code, control is passed to the 

block between rescue and end.
 
For each rescue clause in the begin block, Ruby compares the raised Exception against each 

of the parameters of the...