Sets in Python

Python Methods and Functions

Frozen Sets Frozen Sets are immutable objects that only support methods and operators that produce results without affecting the frozen set or sets to which they apply.

# Python program to show differences
# between normal and frozen set

 
# Same as {& quot; a & quot ;, & quot; b & quot ;, & quot; c & quot;}

normal_set = set ([ " a " , " b " , "c" ])

  
# Adding an element to the regular set is ok

normal_set.add ( "d" )

 

print ( "Normal Set" )

print (normal_set)

 
# Frozen set

frozen_set = frozenset ([ "e" , "f" , "g" ])

 

print ( "Frozen Set" )

print (frozen_set)

 
# Uncommenting below the line will throw an error like
# we're trying to add an item to the frozen set
# frozen_set.add (& quot; h & quot;)

Exit:

 Normal Set set (['a',' c', 'b',' d']) Frozen Set frozenset (['e',' g', ' f']) 

Methods for Sets

1. Add (x) method: adds an x ​​element to install if it is not already in the set.

 people = {"Jay", "Idrish", "Archil"} people.add ( "Daxit") 

- & gt; This will add Daxit to the set of people.

2. union (s) : Returns the union of two sets. Using & # 39; | & # 39; The operator between 2 sets is the same as writing set1.union (set2)

 people = {"Jay", "Idrish", "Archil"} vampires = {"Karan", "Arjun"} population = people.union (vampires) 

OR

 population = people | vampires 

- & gt; The population set will have both human and vampire components

3. The intersect (s): method returns the intersection of two sets. The & # 39; & amp; & # 39; operator can also be used in this case.

 victims = people.intersection (vampires) 

- & gt; The set of victims will contain a common element of humans and vampires

4. Difference method (s): Returns a set containing all the elements of the calling set, but not the second set. We can use the "-" operator here.

 safe = people.difference (vampires) 

OR

 safe = people - vampires 

- & gt; The safe will contain all the elements that humans have, but not vampires.
5. clear () Method: clears the entire set.

 victims.clear () 

- & gt; Cleans up a collection of victims

However, Python collections have two main pitfalls:

  1. Collection does not support elements in any particular order.
  2. Only instances of immutable types can be added to the Python collection.

Set operators

Sets and frozen sets support the following operators :

enter s # content validation

key not in s # non-save validation

s1 == s2 # s1 is equivalent to s2

s1 ! = s2 # s1 is not equivalent to s2

s1 = s2 # s1 is a superset of s2

s1 & gt; s2 # s1 — correct superset of s2

s1 | s2 # combining s1 and s2

s1 & amp; s2 # intersection of s1 and s2

s1 — s2 # a set of elements in s1, but not s2

s1 ˆ s2 # a set of elements in exactly one of s1 or s2

A code snippet to illustrate all Set operations in Python

# Python program to demonstrate how it works # from
# Install in Python

 
# Create two sets

set1 = set ()

set2 = set ()

 
# Adding elements to set1

for i in range ( 1 , 6 ):

set1.add (i)

 
# Adding elements in set2

for i in range ( 3 , 8 ):

set2.add (i)

 

print ( "Set1 =" , set1)

print ( "Set2 =" , set2)

print ( "" )

 
# Union of set1 and set2

set3 = set1 | set2 # set1.union (set2)

print ( " Union of Set1 & amp; Set2: Set3 = " , set3)

 
# Intersection of set1 and set2

set4 = set1 & amp; set2 # set1.intersection (set2)

print ( " Intersection of Set1 & amp; Set2: Set4 = " , set4)

print ( "" )

 
# Check the relationship between set3 and set4

if set3 & gt; set4: # set3.issuperset (set4)

print ( " Set3 is superset of Set4 " )

elif set3 & lt; set4: # set3.issubset (set4)

print ( " Set3 is subset of Set4 " )

else : # set3 == set4

print ( "Set3 is same as Set4" )

  
# display the relationship between set4 and set3

if set4 & lt; set3: # set4.issubset (set3)

print ( " Set4 is subset of Set3 " )

print ( "" )

 
# difference between set3 and set4

set5 = set3 - set4

print ( "Elements in Set3 and not in Set4: Set5 =" , set5)

print ( " " )

 
# checkv if set4 and set5 are disjoint sets

if set4.isdisjoint (set5):

print ( " Set4 and Set5 have nothing in common " )

 
# Removing all values ​​from set5
set5.clear ()

 

print ( "After applying clear on sets Set5:" )

print ( "Set5 =" , set5)

 

Exit:

 ('Set1 =', set ([1, 2, 3, 4, 5])) ('Set2 =', set ([3, 4, 5, 6, 7])) ('Union of Set1 & amp; Set2: Set3 = ', set ([1, 2, 3, 4, 5, 6, 7])) (' Intersection of Set1 & amp; Set2: Set4 = ', set ([3, 4, 5])) Set3 is superset of Set4 Set4 is subset of Set3 ('Elements in Set3 and not in Set4: Set5 =', set ([1, 2, 6, 7])) Set4 and Set5 have nothing in common After applying clear on sets Set5: ('Set5 =', set ([]))