INTRODUCTION TO SETS

In this tutorial we will learn about sets , how to create them,how to add  and remove sets its methods .

sets

Sets

Set is an unordered collection of items  ‘{ }’.  Python set itself is mutable allowing addition and removal of items but the elements are immutable which forbids any change in existing items.

Unlike tuples or lists, Python sets don’t allow multiple occurrences of the same element. Hence, each element in Python sets are unique.

There are two types of built-in set types:

  • set: Python set type is mutable
  • frozen set: Python frozen set type is immutable

How to create Python set

sets are created using set() function.

>>> pst = set( ) #creating empty set

>>> pst = set(‘Python Sets’) #creating set from string

>>> print (pst)

>>>{‘e’,’y’,’S’,’P’,’s’,’h’,’n’,’o’,’ ‘,’t’}

 creating a Python set from a tuple

>>> pst = set((‘a’,’b’,’c’,’d’)) #creating set from a tuple

>>> print (pst)

>>>{‘a’,’b’,’c’,’d’}

>>> pst=set([‘H’,’B’,’O’]) #creating set from a list

>>> pst

>>>{‘H’,’B’,’O’}

How to create sets  from a list containing duplicate items

>> pset=set([‘H’,’B’,’O’,’O’,’B’])

>>> pset

>>>{‘H’,’B’,’O’}

 Adding items in a set

using add( ) and update( ) we add new items to the  set.

  • set( ): to add single item
  • update( ): to add multiple items

Note: Slicing or indexing operator can’t be used in Python sets to add or access members

>>> ps=set([‘a’,’b’,’c’]) #creating a new set

>>> ps.add(‘d’) #Adding a element using add( )

>>> print (ps)

>>>{‘d’,’a’,’b’,’c’}

>>>ps.update([‘e’,’f’]) # Adding multiple items using update( )

>>>print (ps)

>>>{‘e’,’a’,’f’,’b’,’d’,’c’}

How to remove items from a Python set

we can remove item using pop( ) , remove( ) , discard( ) , clear( ) .

  • pop ():-   removes  a item from a Python set.

>>> py_set = set([1,2,3])

>>> py_set.pop()

>>>1

>>>print(py_set)

>>>{2,3}

  • remove():- used to  remove any existing item in the Python set . It will throw and error if the item doesn’t exist in the set.

>>> py_set = set([1,2,3,4])

>>> py_set.remove(3)

>>> print (py_set)

>>>{1,2,4}

  • discard():- used to remove an item from the set. it do not throw an error if the item doesn’t exist in the set.

>>> py_set = set([1,2,3,4])

>>> py_set.discard(2)

>>> print (py_set)

>>>{1,3,4}

  • clear():- it remove all items from  set.

>>> py_set = set([1,2,3])

>>> py_set.clear()

>>> print (py_set)

>>>set()

Iterating through sets

it is done using for loop.

>>>for letters in set(‘hello’):

print(letters)

>>>

h

e

l

l

o

How to copy a set

to copy a set or a item we have copy() function .

>>> py_set1 = set([1,2,3])

>>> py_set = py_set1.copy() #copy items of py_set1 to py_set

>>> print (py_set)

>>>{1,2,3}

Some mathematical sets operations

  • Set Union:

done using union ( ) function  and | .

>>>set1 = {[1,2,3,4,5]}

>>>set2 = {[3,4,5,6,7]}

>>> set3 = set1 | set2 #using |

>>> print (set3)

>>>{[1,2,3,4,5,6,7]}

>>>set3 = set(set1.union(set2)) #using union() function

>>>{1,2,3,4,5,6,7}

  • Set Intersection:

done using intersection( ) function and & operator.

>>> set1 = {[1,2,3,4,5]

>>> set2 = {[3,4,5,6,7.]}

>>> set3 = set1 & set2 #using & operator

>>> print (set3)

>>>{[3,4,5]}

>>>set3 = set(set1.intersection(set2)) #using intersection() function

>>>{3,4,5}

  • Set difference:

done using difference() function  and ( -) operator .

>>> set1 = {[1,2,3,4,5]}

>>> set2 = {[3,4,5,6,7]}

>>> set3 = set1 – set2 #using – operator

>>> print (set3)

>>>{[1,2]}

>>>set3 = set(set1.difference(set2)) #using difference() function

>>>{1,2}

  • Symmetric difference:

    done using Symmetric_difference( ) function and ^ operator.

>>> set1 = {[1,2,3,4,5]}

>>> set2 = {[3,4,5,6,7]}

>>> set3 = set1 ^ set2 #using ^ operator

>>>print (set3)

>>>{[1,2,6,7]}

>>>set3 = set(set1.symmetric_difference(set2)) #using symmetric_difference() function

>>>{1,2,6,7}

  • Subset and Superset :

checked using issubset() function and issuperset( ) function.

>>> X = {[1,2,3]}

>>> Y = {[1,2,3,4,5,6,7]}

>>> X <= Y #checking subset using <= operator

>>>TRUE

>>>X.issubset(Y) #using issubset()

>>>TRUE

>>>X >= Y #checking superset using >= operator

>>>FALSE

>>> Y >= X

>>>TRUE

>>>Y.issuperset(X) #checking superset using issuperset()

>>>TRUE

frozen set

its is same like set , but the difference is  that set is immutable and frozen set are mutable .

it can be create using frozenset()  function.

>>> py_frozenset = frozenset([1,2,3,4,5])

Some set methods

  • set.add( ):- Adds an element to the set.
  • set.clear( ):- Remove all the items from the set.
  • set.copy( ):- Returns a shallow copy of the set. Used for copying.
  • set.difference( ):- Returns the difference of two or more sets.
  • set.discard( ):- Removes an element from a set if it exist in the set.
  • set.intersection( ):- Returns the set of common elements between sets i.e intersection.
  • set.disjoint( ):- Returns true if the sets don’t have elements in common i.e null intersection.
  • set.issubset( ):- Returns true if another set contains all the elements of this set.
  • set.issuperset( ):- Returns true if this set contains all the elements of another set.
  • set.pop( ):- Returns and removes an arbitrary item from the set. Raises error if the set is empty.
  • set.remove( x):- Removes the element x from the set. If x doesn’t exist in the set, it raises error.
  • set.symmetric_difference( ):- Performs symmetric difference and returns the set of all the members of the sets involved excluding the common elements.
  • set.union( x):- Performs and the returns the union of the sets as a set.
  • set.update( ):- Adds multiple items in the set.

http://www.javatpoint.com

https://docs.python.org

%d bloggers like this: