An Introduction to Python Set

A quick overview of python set.

Image by Clker-Free-Vector-Images from Pixabay

Python Set

Set is a data structure in Python, which is unordered and unindexed. The set has no duplicate elements. Every set element is
unique and immutable but the set itself is mutable. That means we can’t change the value of the set element but we can add or remove elements from the set. Sets are written within curly brackets.

Topics covered in this story

Image by Author

Creating Python Set

https://gist.github.com/IndhumathyChelliah/a7997a46b1757b90e55dd4ce6bbe73cc

Accessing Set Elements

Set elements are unordered, so it can’t be accessed. But we can loop through the set element.

https://gist.github.com/IndhumathyChelliah/ca05a65da6f27c9046ccb91f9c4de275

Add Items to Set

We can use add() and update() method to add elements to set. add() used to add one element to set. update() method used to add multiple elements to set.

https://gist.github.com/IndhumathyChelliah/59ee73d2b389efd6ad9027b8b65b9e6c

Note: Both add() and update() methods will modify the original set itself. It won’t return anything. The return type is None.


Removing an Element from the Set

  • remove()
  • discard()
  • pop()
  • clear()
  • del

remove()

remove() will remove the specified element from the set. If the element is not in set means, it will raise an error.

discard()

discard() will also remove the specified element from the set. If the element is not in set means, it won’t raise an error.

pop()

Since the set is unordered, pop() will remove any element from the set.

clear()

clear() will empty the set.

del

del keyword will delete the set itself.

https://gist.github.com/IndhumathyChelliah/853f207378a46d2bd3b4622387d9861a

Note: remove(),discard(),clear() methods will modify the original set itself.It won’t return anything.Return Type is None.
pop() method will return the removed element.Return value is removed element.

copy()

copy method will return a copy of the set.

https://gist.github.com/IndhumathyChelliah/fd2f9891a4387e1884e435cb2e935ba0


Mathematical Set Operations

  1. union(): Return new set containing union of both set.Union means return all elements from both sets.Union is performed by |operator or by union() method
  2. intersection(): Return new set containing intersection of both set.Intersection means returns only common element between both set.Intersection is performed by & operator or by intersection() method.

https://gist.github.com/IndhumathyChelliah/26f4fc19600e518473c6d8b87eba26f1

3. difference(): Returns a new set containing the difference between both sets. A-B means returns elements from set A which is not present in set B. The difference can be performed by -operator or using the difference() method.

4. difference_update(): Removes the element from the set which is also present in the other set.It is performed by using -= operator or by using the difference_update() method. The return type is None. It will modify the original set itself.

https://gist.github.com/IndhumathyChelliah/8d349f5102b3ad2c317f6fac299cb2eb

5. symmetric_difference(): Returns a new set containing elements from both sets excluding common elements in both sets.It is performed by using ^ operator or by using the symmetric_difference() method.

6. symmetric_difference_update(): Updates the set by keeping elements in both sets excluding common elements in both sets. It is performed by using ^= operator or by using the symmetric_difference_update() method. The return type is None. It will modify the original set itself.

https://gist.github.com/IndhumathyChelliah/d37e0dfbf38575cac1980de4bfc4f4ef

7.intersection_update(): Updates the set with elements that are common to both sets.It is performed by &= operator or by using intersection_update() method.Return Type is None. It will update the original set itself.

https://gist.github.com/IndhumathyChelliah/cc782fa30ce1673e3ae3c57786c9fe2e

8. isdisjoint(): Returns True if both sets have no common elements.Returns False if both sets have common elements.

https://gist.github.com/IndhumathyChelliah/9872b8f19722f1f51238526d60bc53e2

9. issubset(): Returns True if all elements in this set are present in the specified set or otherwise Returns False.

https://gist.github.com/IndhumathyChelliah/0e498fa9126bec1c0718a638f43abafc

10. issuperset(): Returns True if all elements in the specified set are present in the original set or otherwise Returns False.

https://gist.github.com/IndhumathyChelliah/ae96f26502217c9ac0e0a28d6baa93ee


Membership Operations

We can check whether an item is present in the set or not by using in keyword.

https://gist.github.com/IndhumathyChelliah/89f12a6b737c9634c7f16fe32ac4e75f


Built-in Functions -Set

  1. len(): Returns the number of elements in the set
  2. min(): Returns the smallest element in the set
  3. max(): Returns the largest element in the set
  4. sum(): Returns the sum of all elements in the set.

https://gist.github.com/IndhumathyChelliah/15102d3715838a75cd397081bfe81eb5

5. sorted(): Returns a new list containing sorted elements. Since the set is unordered, the return type is List.

https://gist.github.com/IndhumathyChelliah/8bb8891feeda8da5e7302b3ab4cc73a4

6. all(): Returns True if all elements in the set are True or if the set is empty.

7. any(): Returns True if any element in the set is True.If the set is empty returns False

https://gist.github.com/IndhumathyChelliah/5ff5583b11a74c7ad193440ce2fd8a7a


Conclusion:

  • Set methods which will modify the original set.
    Return Type is None.
    add(),update(),remove(),discard(),clear(),difference_update(), symmetric_difference_update(),intersection_update()
  • Return Type Boolean
    isdisjoint(),issuperset(),issubset(),any(),all()
  • Return Type List
    sorted()
  • Return type removed element
    pop()
  • Returns new set and won’t modify the original set.
    union(),intersection(),difference(),symmetric_difference()

My other blogs on Python data structures

An Introduction to Python List

String Methods in Python

An Introduction to Python Tuple

An Introduction to Python Dictionary

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s