Python Set

Python Set

Python Set is a collection of elements.

Elements of a Python Set are unordered. There is no indexing mechanism in which you can access the elements of a Set.

Python Set elements are unique. You cannot have two elements with same reference or value in some cases.

Python Set is mutable. You can change/modify the elements in a Set.

Initialize Python Set

You can initialize a Python Set using flower braces {}. Elements you would like initialize in the set go inside these braces. If there are multiple elements, they should be separated from each other using comma.

Following is a simple example, demonstrating the initialization of an empty set, set with single element, and set with multiple elements.

Python Program

#empty set
set_1 = {}

#set with single item
set_2 = {54}

#set with multiple items
set_3 = {32, 41, 29, 85}

print(set_1)
print(set_2)
print(set_3)
Run this program

Output

{}
{54}
{32, 41, 85, 29}

Python Set can have only Unique Values

We already mentioned that elements of Python Set should be unique.

In this example, let us try to initialize a Python Set with some duplicate elements and observe what happens.

Python Program

#set with duplicate items
set_3 = {32, 41, 29, 41, 32, 85}

print(set_3)
Run this program

Output

{32, 41, 85, 29}

Only unique elements are initialized to the set and the duplicates are discarded.

Python Set Operations

You can perform many operations on a Python Set. Following are the tutorials to these Python Set Operations.

  • add() – Adds an element (passed as argument) to the set. If element is already present, set remains unchanged.
  • clear() – Removes all the elements from the set. And the set becomes empty.
  • copy() – Returns a copy of the set. Helpful when you would like to keep an original copy of the Set.
  • difference() – Returns a set containing the difference between two or more sets.
  • difference_update() – Removes the items in this set that are also included in another, specified set.
  • discard() – Remove the specified item.
  • intersection() – Returns a set, that is the intersection of two other sets.
  • intersection_update() – Removes the items in this set that are not present in other, specified set(s).
  • isdisjoint() – Returns whether two sets have a intersection or not.
  • issubset() – Returns whether another set contains this set or not.
  • issuperset() – Returns whether this set contains another set or not.
  • pop() – Removes an element from the set. Which element it pops is uncertain.
  • remove() – Removes the specified element(passed as argument) .
  • symmetric_difference() – Returns a set with the symmetric differences of two sets
  • symmetric_difference_update() – inserts the symmetric differences from this set and another
  • union() – Return a set containing the union of sets. Only the unique elements of the sets provided.
  • update() – Update the set with the union of this set and others. This original set is modified.

Summary

In this tutorial of Python Examples, we learned what a Python Set is, how to initialize it, and different methods available to apply on a Python Set.