top
upGrad KnowledgeHut SkillFest Sale!

Search

Python Tutorial

Set is another collection data type in Python. However, it is not sequence, such as List or Tuple. It is an unordered collection of items. Hence, its constituent items don’t follow zero based positional index. Set also doesn’t allow duplicate objects to be stored, where as in List and Tuple, same object can appear more than once.Set theory is an Important branch of Mathematics. Set data type is a Python implementation of set as defined in Mathematics. The built-in Set class has defined in it lot of suitable methods to perform mathematical set operations like union, intersection, difference etc.A Set object contains one or more items, which may be of same or different type, separated by comma and enclosed in curly brackets {}. As mentioned earlier, Set doesn’t store duplicate objects. So, even if same object is put more than once inside curly brackets, only one copy appears in the object.In [1]: s1={10,34,32,11,25} s1 Out[1]: {10, 11, 25, 32, 34} In [2]: s1={100, "Hundred", 10.25, 10.100+5j} s1 Out[2]: {(10.1+5j), 10.25, 100, 'Hundred'}The set object can store a collection of only immutable objects such as number, string and tuple. If we try to include a list or dict object in curly brackets, Python raises TypeError saying that these are unhashable.Hashing is a mechanism in computer science which enables quicker searching of objects in computer’s memory. Python interpreter uses its own hashing algorithm to store items in set. As a result, items may not appear in the same sequence as used in its literal representation. Python optimizes the structure for improved performance of set operations.In [3]: s2={{1:11,2:22},(10,20),{1,2,3}} s2 --------------------------------------------------------------------------- TypeError                                 Traceback (most recent call last) <ipython-input-3-1ae95440415b> in <module>() ----> 1 s2={{1:11,2:22},(10,20),{1,2,3}}       2 s2 TypeError: unhashable type: 'dict'set() functionPython has an in-built function set() using which set object can be constructed out of any sequence like string, list or tuple object. In fact this function acts as constructor of built-in set class.When used without arguments, set() produces empty set.In [4]: s2=tuple(range(5)) s2 Out[4]: (0, 1, 2, 3, 4) In [5]: s2=set() s2 Out[5]: set() In [6]: s3=set("Hello World") s3 Out[6]: {' ', 'H', 'W', 'd', 'e', 'l', 'o', 'r'} In [7]: s4=set([54,23,45,11,23,76,11]) s4 Out[7]: {11, 23, 45, 54, 76}Methods of set classEven though mutable objects are not stored in a set, set itself is a mutable object. A set object can be modified by add(), update(), remove() and discard() methods.add()Add an element to a set.This has no effect if the element is already present.pop()Remove and return an arbitrary set element.RaisesKeyError if the set is empty.remove()Remove an element from a set.If the element is not a member, raise a KeyError.discard()Remove an element from a set if it is a member.If the element is not a member, do nothing.update()Update a set with the union of itself and others.In [8]: s1={"Django", "Flask", "Pyramid"} s1 Out[8]: {'Django', 'Flask', 'Pyramid'} In [9]: s1.add("Turbogears") s1 Out[9]: {'Django', 'Flask', 'Pyramid', 'Turbogears'} In [10]: s2={"NumPy", "SciPy","IPython"} s1.update(s2) s1 Out[10]: {'Django', 'Flask', 'IPython', 'NumPy', 'Pyramid', 'SciPy', 'Turbogears'}The remove(), pop() and discard() methods drop an item from the set. pop() method drops a random item whereas remove() and discard() need an item to be dropped as argument. If not present in the set, remove() method raises KeyError exception.In [11]: s1.remove('SciPy') s1 Out[11]: {'Django', 'Flask', 'IPython', 'NumPy', 'Pyramid', 'Turbogears'} In [12]: s1.remove("Kivy") --------------------------------------------------------------------------- KeyError                                  Traceback (most recent call last) <ipython-input-12-29217ddff50a> in <module>() ----> 1 s1.remove("Kivy") KeyError: 'Kivy' In [13]: s1.pop() Out[13]: 'Flask' In [14]: s1 Out[14]: {'Django', 'IPython', 'NumPy', 'Pyramid', 'Turbogears'} In [15]: s1.discard("Flask") In [16]: s1 Out[16]: {'Django', 'IPython', 'NumPy', 'Pyramid', 'Turbogears'}Methods for Set operations:As mentioned earlier, set data type in Python implements set as defined in mathematics. Various Set operations like union, intersection, difference and symmetric difference can be performed using Python’s set object.Set operations can be summarized by following table:UnionUnion of two sets is a set of all elements in both.IntersectionIntersection of two sets is a set containing elements common to bothDifferenceDifference of two sets results in a set containing elements only in first but not in second set.Symmetric differenceResult of Symmetric difference is a set consisting of elements in both sets excluding common elementsFollowing methods in set class perform above operations:difference()Return the difference of two or more sets as a new set.difference_update()Remove all elements of another set from this set.intersection()Return the intersection of two sets as a new set.intersection_update()Update a set with the intersection of itself and another.symmetric_difference()Return the symmetric difference of two sets as a new set.  symmetric_difference_update()Update a set with the symmetric difference of itself and another.union()Return the union of sets as a new setIn [30]: s1={10,20,30,40,50} s2=set('abcde') s3=s1.union(s2) s3 Out[30]: {10, 20, 30, 40, 50, 'a', 'b', 'c', 'd', 'e'} In [31]: s1={10,20,30,40,50} s2={0,20,40,60} s3=s1.intersection(s2) s3 Out[31]: {20, 40} In [32]: s1={10,20,30,40,50} s2={0,20,40,60} s3=s1.difference(s2) s3 Out[32]: {10, 30, 50} In [33]: s1={10,20,30,40,50} s2={0,20,40,60} s3=s1.symmetric_difference(s2) s3 Out[33]: {0, 10, 30, 50, 60}Above methods (union, intersection, difference and symmetric_difference) return a new set object. As against these methods intersection_update, difference_update and symmetric_difference_update perform their respective operations in place. It means s1.intersection_update(s2) will change s1 to the result of intersection with s2.In [34]: s1={10,20,30,40,50} s2={20,40} s1.difference_update(s2) s1 Out[34]: {10, 30, 50} In [35]: s1={10,20,30,40,50} s2={0,20,40,60} s1.symmetric_difference_update(s2) s1 Out[35]: {0, 10, 30, 50, 60} In [36]: s1={10,20,30,40,50} s2={0,20,40,60} s1.intersection_update(s2) s1 Out[36]: {20, 40}Boolean set methodsFollowing methods decide whether a set is a super set, sub set or two are disjoint.isdisjoint()Return True if two sets have a null intersection.issubset()Report whether another set contains this set.  issuperset()Report whether this set contains another set.In [37]: s1={10,20,30,40,50} s2={20,40} s2.issubset(s1) Out[37]: True In [38]: s1.issuperset(s2) Out[38]: True In [39]: s1.isdisjoint(s2) Out[39]: False In [40]: s2.isdisjoint(s1) Out[40]: Falseclear() method drops all items in the set keeping object intact. The del keyword on the other hand removes object from memory.In [41]: s1.clear() s1 Out[41]: set() In [42]: del s2 s2 --------------------------------------------------------------------------- NameError                                Traceback (most recent call last) <ipython-input-42-9b4ca538b1cf> in <module>()     1 del s2 ----> 2 s2 NameError: name 's2' is not definedFrozen setA frozenset is different from set in the sense that it is immutable. It means that any operations that change the composition of frozenset object will raise exception.In [46]: s1=frozenset(["Django", "Flask", "Pyramid"]) s1.add("TurboGears") --------------------------------------------------------------------------- AttributeError                           Traceback (most recent call last) <ipython-input-46-bfd9ac8ee662> in <module>()       1 s1=frozenset(["Django", "Flask", "Pyramid"]) ----> 2 s1.add("TurboGears") AttributeError: 'frozenset' object has no attribute 'add' In [47]: s1.pop() --------------------------------------------------------------------------- AttributeError                            Traceback (most recent call last) <ipython-input-47-095118de218b> in <module>() ----> 1 s1.pop() AttributeError: 'frozenset' object has no attribute 'pop'However, set operations such as union, intersection etc. can be performed on frozenset object.
logo

Python Tutorial

Python - Sets

Set is another collection data type in Python. However, it is not sequence, such as List or Tuple. It is an unordered collection of items. Hence, its constituent items don’t follow zero based positional index. Set also doesn’t allow duplicate objects to be stored, where as in List and Tuple, same object can appear more than once.

Set theory is an Important branch of Mathematics. Set data type is a Python implementation of set as defined in Mathematics. The built-in Set class has defined in it lot of suitable methods to perform mathematical set operations like union, intersection, difference etc.

A Set object contains one or more items, which may be of same or different type, separated by comma and enclosed in curly brackets {}. As mentioned earlier, Set doesn’t store duplicate objects. So, even if same object is put more than once inside curly brackets, only one copy appears in the object.

In [1]:
s1={10,34,32,11,25}
s1
Out[1]:
{10, 11, 25, 32, 34}
In [2]:
s1={100, "Hundred", 10.25, 10.100+5j}
s1
Out[2]:
{(10.1+5j), 10.25, 100, 'Hundred'}

The set object can store a collection of only immutable objects such as number, string and tuple. If we try to include a list or dict object in curly brackets, Python raises TypeError saying that these are unhashable.

Hashing is a mechanism in computer science which enables quicker searching of objects in computer’s memory. Python interpreter uses its own hashing algorithm to store items in set. As a result, items may not appear in the same sequence as used in its literal representation. Python optimizes the structure for improved performance of set operations.

In [3]:
s2={{1:11,2:22},(10,20),{1,2,3}}
s2
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-3-1ae95440415b> in <module>()
----> 1 s2={{1:11,2:22},(10,20),{1,2,3}}
      2 s2

TypeError: unhashable type: 'dict'

set() function

Python has an in-built function set() using which set object can be constructed out of any sequence like string, list or tuple object. In fact this function acts as constructor of built-in set class.When used without arguments, set() produces empty set.

In [4]:
s2=tuple(range(5))
s2
Out[4]:
(0, 1, 2, 3, 4)
In [5]:
s2=set()
s2
Out[5]:
set()
In [6]:
s3=set("Hello World")
s3
Out[6]:
{' ', 'H', 'W', 'd', 'e', 'l', 'o', 'r'}
In [7]:
s4=set([54,23,45,11,23,76,11])
s4
Out[7]:
{11, 23, 45, 54, 76}

Methods of set class

Even though mutable objects are not stored in a set, set itself is a mutable object. A set object can be modified by add(), update(), remove() and discard() methods.

add()

Add an element to a set.This has no effect if the element is already present.
pop()
Remove and return an arbitrary set element.RaisesKeyError if the set is empty.
remove()
Remove an element from a set.If the element is not a member, raise a KeyError.
discard()
Remove an element from a set if it is a member.If the element is not a member, do nothing.
update()
Update a set with the union of itself and others.
In [8]:
s1={"Django", "Flask", "Pyramid"}
s1
Out[8]:
{'Django', 'Flask', 'Pyramid'}
In [9]:
s1.add("Turbogears")
s1
Out[9]:
{'Django', 'Flask', 'Pyramid', 'Turbogears'}
In [10]:
s2={"NumPy", "SciPy","IPython"}
s1.update(s2)
s1
Out[10]:
{'Django', 'Flask', 'IPython', 'NumPy', 'Pyramid', 'SciPy', 'Turbogears'}

The remove(), pop() and discard() methods drop an item from the set. pop() method drops a random item whereas remove() and discard() need an item to be dropped as argument. If not present in the set, remove() method raises KeyError exception.

In [11]:
s1.remove('SciPy')
s1
Out[11]:
{'Django', 'Flask', 'IPython', 'NumPy', 'Pyramid', 'Turbogears'}
In [12]:
s1.remove("Kivy")
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-12-29217ddff50a> in <module>()
----> 1 s1.remove("Kivy")
KeyError: 'Kivy'
In [13]:
s1.pop()
Out[13]:
'Flask'
In [14]:
s1
Out[14]:
{'Django', 'IPython', 'NumPy', 'Pyramid', 'Turbogears'}
In [15]:
s1.discard("Flask")
In [16]:
s1
Out[16]:
{'Django', 'IPython', 'NumPy', 'Pyramid', 'Turbogears'}

Methods for Set operations:

As mentioned earlier, set data type in Python implements set as defined in mathematics. Various Set operations like union, intersection, difference and symmetric difference can be performed using Python’s set object.

Set operations can be summarized by following table:

UnionUnion of two sets is a set of all elements in both.Union of two sets is a set of all elements in both.
Intersection

Intersection of two sets is a set containing elements common to bothIntersection of two sets is a set containing elements common to both

Difference

Difference of two sets results in a set containing elements only in first but not in second set.elements only in first but not in second set

Symmetric difference

Result of Symmetric difference is a set consisting of elements in both sets excluding common elementselements in both sets excluding common elements

Following methods in set class perform above operations:

difference()Return the difference of two or more sets as a new set.
difference_update()Remove all elements of another set from this set.
intersection()Return the intersection of two sets as a new set.
intersection_update()Update a set with the intersection of itself and another.
symmetric_difference()Return the symmetric difference of two sets as a new set.  
symmetric_difference_update()Update a set with the symmetric difference of itself and another.
union()Return the union of sets as a new set
In [30]:
s1={10,20,30,40,50}
s2=set('abcde')
s3=s1.union(s2)
s3
Out[30]:
{10, 20, 30, 40, 50, 'a', 'b', 'c', 'd', 'e'}
In [31]:
s1={10,20,30,40,50}
s2={0,20,40,60}
s3=s1.intersection(s2)
s3
Out[31]:
{20, 40}
In [32]:
s1={10,20,30,40,50}
s2={0,20,40,60}
s3=s1.difference(s2)
s3
Out[32]:
{10, 30, 50}
In [33]:
s1={10,20,30,40,50}
s2={0,20,40,60}
s3=s1.symmetric_difference(s2)
s3
Out[33]:
{0, 10, 30, 50, 60}

Above methods (union, intersection, difference and symmetric_difference) return a new set object. As against these methods intersection_update, difference_update and symmetric_difference_update perform their respective operations in place. It means s1.intersection_update(s2) will change s1 to the result of intersection with s2.

In [34]:
s1={10,20,30,40,50}
s2={20,40}
s1.difference_update(s2)
s1
Out[34]:
{10, 30, 50}
In [35]:
s1={10,20,30,40,50}
s2={0,20,40,60}
s1.symmetric_difference_update(s2)
s1
Out[35]:
{0, 10, 30, 50, 60}
In [36]:
s1={10,20,30,40,50}
s2={0,20,40,60}
s1.intersection_update(s2)
s1
Out[36]:
{20, 40}

Boolean set methods

Following methods decide whether a set is a super set, sub set or two are disjoint.

isdisjoint()Return True if two sets have a null intersection.
issubset()Report whether another set contains this set.  
issuperset()Report whether this set contains another set.
In [37]:
s1={10,20,30,40,50}
s2={20,40}
s2.issubset(s1)
Out[37]:
True
In [38]:
s1.issuperset(s2)
Out[38]:
True
In [39]:
s1.isdisjoint(s2)
Out[39]:
False
In [40]:
s2.isdisjoint(s1)
Out[40]:
False

clear() method drops all items in the set keeping object intact. The del keyword on the other hand removes object from memory.

In [41]:
s1.clear()
s1
Out[41]:
set()
In [42]:
del s2
s2
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-42-9b4ca538b1cf> in <module>()
      1 del s2
----> 2 s2

NameError: name 's2' is not defined

Frozen set

A frozenset is different from set in the sense that it is immutable. It means that any operations that change the composition of frozenset object will raise exception.

In [46]:
s1=frozenset(["Django", "Flask", "Pyramid"])
s1.add("TurboGears")
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-46-bfd9ac8ee662> in <module>()
      1 s1=frozenset(["Django", "Flask", "Pyramid"])
----> 2 s1.add("TurboGears")
AttributeError: 'frozenset' object has no attribute 'add'
In [47]:
s1.pop()
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-47-095118de218b> in <module>()
----> 1 s1.pop()

AttributeError: 'frozenset' object has no attribute 'pop'

However, set operations such as union, intersection etc. can be performed on frozenset object.

Leave a Reply

Your email address will not be published. Required fields are marked *

Comments

Eula

This is my first time here. I am truly impressed to read all this in one place.

Jaypee Dela Cruz

Thank you for your wonderful codes and website, you helped me a lot especially in this socket module. Thank you again!

lucky verma

Thank you for taking the time to share your knowledge about using python to find the path! Your insight and guidance is greatly appreciated.

Pre Engineered Metal Building

Usually I by no means touch upon blogs however your article is so convincing that I by no means prevent myself to mention it here.

Pre Engineered Metal Building

Usually, I never touch upon blogs; however, your article is so convincing that I could not prevent myself from mentioning how nice it is written.

Suggested Tutorials

Swift Tutorial

Introduction to Swift Tutorial
Swift Tutorial

Introduction to Swift Tutorial

Read More

R Programming Tutorial

R Programming

C# Tutorial

C# is an object-oriented programming developed by Microsoft that uses the .Net Framework. It utilizes the Common Language Interface (CLI) that describes the executable code as well as the runtime environment. C# can be used for various applications such as web applications, distributed applications, database applications, window applications etc.For greater understanding of this tutorial, a basic knowledge of object-oriented languages such as C++, Java etc. would be beneficial.
C# Tutorial

C# is an object-oriented programming developed by Microsoft that uses ...

Read More