Sets data type Python

The data type sets is a builtin data type in Python which behave like the Mathematical set.The set object will hold no duplicate elements,all the elements must be unique.Since the set is made to behave like the Mathematical set the Python set support operation like like union, intersection, difference, and symmetric difference.

Creating set

To create set we will use the curly braces ‘{}’ or you can also use the function set().Note since duplicate elements are not allowed if any duplicate elements is found they are remove form the set object.To create an empty set we will use the function set() not ‘{ }’.Using empty curly brace ‘{}’ does not create an empty set it rather create something else known as dictionary(we will discuss about it later).

>>> st={ 23 ,23 , 45 ,67 ,89 }
>>> st
{89, 67, 45, 23} #duplicate element 23 is removed
>>> st1={ 'Happy' , 'Text' , 34 , 'A' , 90.345 , 89}
>>> st1
{34, 'A', 'Text', 89, 'Happy', 90.345} 
>>> ss=set() #empty set created
>>> ss
set()

If we output the elements of the set the set doe not arrange it in some order the elements are arranges randomly.

>>> st={ 23 ,23 , 45 ,67 ,89 }
>>> st
{89, 67, 45, 23}  
>>> st4={ 6 , 4 , 3, 5 , 5 }
>>> st4
{3, 4, 5, 6}
>>> st5={ 23 ,67 ,10 , 1000 ,1 }
>>> st5
{1, 67, 1000, 10, 23} 

If you look at the example above the ‘st’ output the elements in descending order but if you look at the output of st4 the elements are arranged in ascending order and if we look at the output of ‘st5’ the elements are arranged in random order,neither ascending nor descending.Hence we can conclude that the elements are arranged in random order.

We can also create set from list or tuple or any iterable sequence.

>>> st=set([23 , 56 , 'new' , 89.345 ]) #set created from list
>>> st
{56, 'new', 89.345, 23}
>>> stt=set( ( "text" , 78.35 , 9023) ) #set created from tuple
>>> stt
{78.35, 9023, 'text'}

Some points to note

i)We cannot use list as an element of set

Lists cannot be used as an element of set,if we try to use it we will get an error message.

>>> sls={ [34] , 'Text' , [34 ,56] , 90}
Traceback (most recent call last):
  File "<pyshell#24>", line 1, in <module>
    sls={ [34] , 'Text' , [34 ,56] , 90}
TypeError: unhashable type: 'list' 
>>> #another example
>>> sls={ [34] , 0 , 'heap' }
Traceback (most recent call last):
  File "<pyshell#27>", line 1, in <module>
    sls={ [34] , 0 , 'heap' }
TypeError: unhashable type: 'list'

To know why we cannot use list as an element of set look at the last line in each of the error message which says ‘TypeError: unhashable type: ‘list’‘.This means the list type cannot be hashed,what does hash or unhashable or unhashability mean is discussed in another post.


ii)Performing union,intersection, difference, and symmetric difference operation

Let us perform some operation related to sets.

>>> s1={ 23 , 67 , 89 , 90 , 1000 }
>>> s2={ 23 , 55 , 78 , 89 , 100  }
>>> s1 | s2  #elements in s1 or s2 or both,same as union
{67, 100, 1000, 78, 55, 23, 89, 90}
>>>> s1 & s2 #elements in both s1 or s2,same as intersection
{89, 23}
>>> s1 - s2 #elements in s1 but not in s2,same as difference
{1000, 90, 67}
>>> s1 ^ s2 #elements in both s1 and s2 but not both,symmetric difference
{67, 100, 55, 1000, 90, 78} 

There is another set type known as frozenset,for more information on frozenset visit Frozenset type .



Leave a Reply

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