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

Understanding ASCII / Unicode character encoding format

Computer stores everything in binary format. The process of converting a value into binary is called Encoding and the the process of converting value from binary is called Decoding. value --> binary  ::     Encoding  binary --> value   ::    Decoding for example: A number 12 is stored in its binary format as below.               12 ---> 00001100 but how are the characters are stored? If we want to store a character 'a' what formatting should be used ? It easiest way is to map the characters to number and store the binary format of that number. So, the character 'a' is represented as number 97 is stored as 1100001.  ASCII: In ASCII encoding format, 128 unique characters are identified and mapped with numbers (mostly English). One byte is used to represent all the characters. It uses 7 bits to represent numeric value 0 to 127 (total 128 characters).  The most significant bit is a...