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 6 - Blocks , lambda, procs and closure

 Blocks , lambda, procs  and closure Table of content  1. Blocks 2. Lambda 3. Procs 4. Closure Blocks  Ruby blocks are little anonymous functions that can be passed into methods. Blocks are enclosed in a do-end statement or between brackets {} 
. Blocks can have multiple arguments
. The argument names are defined between two pipe | characters. Blocks are typically used with ‘each’ method which iterates over a list. Syntax of block using {} ['List of items'].each { | block arguments|  block body }  Syntax of block using do-end ['List of items'].each do | block arguments |      # block body end Example of block declared as do-end with each method.   [ 1 , 2 , 3 ].each do |num| puts num end     Output   $ ruby block_with_each.rb 1 2 3 $    Blocks can also be saved in variables or passed as argument to another function.   yield is a Ruby keyword that is used to call a block. When you use the yield keyword, the code inside the block will run. Example of saving a bl