Sets in Python - IT magazine

IT magazine

Knowledge that matters

Sets in Python

Share This


Sets:
A Set is an unordered collection data type that is iterable, mutable, and has no duplicate elements. Python’s set class represents the mathematical notion of a set. The major advantage of using a set, as opposed to a list, is that it has a highly optimized method for checking whether a specific element is contained in the set. This is based on a data structure known as a hash table.  The order of elements in a set is undefined though it may consist of various elements. Elements of a set can be added and deleted, elements of the set can be iterated, and various standard operations (union, intersection, difference) can be performed on sets.

Sets can be created by using the built-in set() function with an iterable object or a sequence by placing the sequence inside curly braces, separated by ‘comma’. A set contains only unique elements but at the time of set creation, multiple duplicate values can also be passed. Order of elements in a set is undefined and is unchangeable. Type of elements in a set need not be the same, various mixed up data type values can also be passed to the set. A set cannot have mutable elements like a list, set or dictionary, as its elements.

Frozen sets:
Frozen sets in Python are immutable objects that only support methods and operators that produce a result without affecting the frozen set or sets to which they are applied. While elements of a set can be modified at any time, elements of the frozen set remain the same after creation. If no parameters are passed, it returns an empty frozenset.

Set Methods
FUNCTION
DESCRIPTION
add()
Adds an element to a set
remove()
Removes an element from a set. If the element is not present in the set, raise a KeyError
clear()
Removes all elements form a set
copy()
Returns a shallow copy of a set
pop()
Removes and returns an arbitary set element. Raise KeyError if the set is empty
update()
Updates a set with the union of itself and others
union()
Returns the union of sets in a new 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 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
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

Operators for Sets
Sets and frozen sets support the following operators:

key in s
 Checks whether the key is present in the set or not. Returns True if the key is present in the key otherwise False is returned.
key not in s
 Checks whether the key is not present in the set or not. Returns True if the key is not present in the key otherwise False is returned.
s1 == s2
 Checks the equality of two sets.
s1 != s2
 Checks the inequality of two sets.
s1 <= s2
Returns True if s1 is subset of s2
s1 < s2
Returns True if s1 is proper subset of s2
 s1 >= s2
ReturnsTrue if s1 is superset of s2
s1 > s2
Returns True if s1 is proper superset of s2
s1 | s2
Returns a set with all the allements of s1 and s2
s1 & s2
Returns a set contianing elements present in both the sets
s1 – s2
Returns a  set with  elements present in s1 but not s2
s1 ˆ s2
Returns a set with elements in both the set excluding the common elements


No comments:

Post a Comment