Python Working With Set Data Type

Python Working With Set Data Type

Lets start working with set data type in python. Set is a heterogeneous un-ordered set of elements with no repetition. set data structure is also just like frozensets. The only difference between sets and frozensets is sets are immutable where as frozensets are not mutable in nature. we can use sets to checking of membership and eliminating the repetitive/duplicate elements. Sets support operations like union, symmetric difference, intersection and difference.

Basic operations of Sets in python

  • Create a set with and without elements
# create empty set
s2 = set()  # set() -> new empty set object
# create set with elements
s1 = {1,2,3,4,"hi", 1.24}
s2 = set([1,2,3,4,"hi", 1.24])  # set(iterable) -> new set object
print(type(s1))
# Output: <class 'set'>
print(type(s2))
# Output: <class 'set'>
  • Add element to set
s1 = {1,2,3, 3, 3}
s1.add(100)
s1.add(98)
print(s1)
# Output: {1, 2, 3, 100, 98}

As we can see above set do not contains duplicate elements and it is a unordered set of elements.

  • Remove element from set
s1 = {1,2,100}
s1.remove(100)
print(s1)
# Output: {1,2}

remove method in set can take the element and removes from the set.

Working with Sets methods in python

  • Working with "clear" method in sets in python
s1 = {1,2,3}
s1.clear()
print(s1)
# Output: {}

Remove all elements from this set. It means simply making the set to empty.

  • Working with "copy" method in sets in python
s1 = {1,2,3}
s2 = s1.copy()
s2.add(100)
print(s1)
# output: {1,2,3}
print(s2)
# Output: {1,2,3,100}
print(id(s1), id(s2))
# Output: 139732324395720 139732306460744

Return a shallow copy of a set.

  • Working with "difference" method in sets in python
s1 = {1,2,3, 100, 200}
s2 = {2, 100}
s3 = s1.difference(s2)
print(s3)
# Output: {200, 1, 3} 

Return the difference of two or more sets as a new set. There will be no affect on the operating sets(ie. s1, s2). i.e. all elements that are in this set but not the others.

  • Working with "difference_update" method in sets in python
s1 = {1,2,3, 100, 200}
s2 = {2, 100}
s1.difference_update(s2)
print(s1)
# Output: {1, 3, 200}

Remove all elements of another set from this set. The method finds the difference and updates the result in the set s1.

  • Working with "discard" method in sets in python
s1 = {1,2,3, 100, 200}
s1.discard(100)
print(s1)
# output: {1,2,3, 200}

Remove an element from a set if it is a member. If the element is not a member, do nothing.

  • Working with "intersection" method in sets in python
s1 = {1,2,3, 100, 200}
s2 = {100, 200}
s3 = s1.intersection(s2)
print(s3)
# Output: {200, 100}

Return the intersection of two or more sets as a new set(i.e. elements that are common to all of the sets). No affect on sets on s1 and s2.

  • Working with "intersection_update" method in sets in python
s1 = {1,2,3, 100, 200}
s2 = {100, 200}
s1.intersection_update(s2)
print(s1)
# Output: {200, 100}

Update a set with the intersection of itself and another.

  • Working with "isdisjoint" method in sets in python
s1 = {1,2,3, 100, 200}
s2 = {500, 400}
result = s1.isdisjoint(s2)
print(result)
# Output: True
s3 = {100, 200}
result = s1.isdisjoint(s3)
print(result)
# Output: False

Return True if two sets have a null intersection.

  • Working with "issubset" method in sets in python
s1 = {1,2,3, 100, 200}
s2 = {500, 400}
result = s1.issubset(s2)
print(result)
# Output: False
s3 = {1,2,3}
result = s3.issubset(s1)
print(result)
# Output: True

Report whether another set contains this set.

  • Working with "issuperset" method in sets in python
s1 = {1,2,3, 100, 200}
s2 = {500, 400}
result = s1.issuperset(s2)
print(result)
# Output: False
s3 = {1,2,3}
result = s1.issuperset(s3)
print(result)
# Output: True

Report whether this set contains another set.

  • Working with "pop" method in sets in python
s1 = {1,2,3, 100, 200}
s1.pop()
print(s1)
# Output: {1, 2, 3, 100}
s1 = {} 
s1.pop()
# Output: KeyError: 'pop from an empty set'

Remove and return an arbitrary set element. Raises KeyError if the set is empty.

  • Working with "remove" method in sets in python
s1 = {1,2,3,100,200}
s1.remove(100)
print(s1)
# output: {200, 1, 2, 3}
s1.remove(500)
# output: KeyError: 500

Remove an element from a set; it must be a member. If the element is not a member, raise a KeyError.

  • Working with "symmetric_difference" method in sets in python
s1 = {1,2,3,100,200}
s2 = {5,6,7}
s3 = s1.symmetric_difference(s2)
print(s3)
# Output: {1, 2, 3, 100, 5, 6, 7, 200}

Return the symmetric difference of two sets as a new set.(i.e. all elements that are in exactly one of the sets.)

  • Working with "symmetric_difference_update" method in sets in python
s1 = {1,2,3,100,200}
s2 = {5,6,7}
s1.symmetric_difference(s2)
print(s1)
# Output: {1, 2, 3, 100, 5, 6, 7, 200}

Update a set with the symmetric difference of itself and another.

  • Working with "union" method in python
s1 = {1,2,3,100,200}
s2 = {5,6,7}
s3 = s1.union(s2)
print(s3)
# Output: {1, 2, 3, 100, 5, 6, 7, 200} 

Return the union of sets as a new set. (i.e. all elements that are in either set.)

  • Working with "update" method in python
s1 = {1,2,3,100,200}
s2 = {5,6,7}
s1.update(s2)
print(s1)
# Output: {1, 2, 3, 100, 5, 6, 7, 200}

Update a set with the union of itself and others. References: https://docs.python.org/3/tutorial/datastructures.html#sets