Understanding Set data structure
Table of content
- Set
- How to define set in python
- Accessing set elements
- 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