31 - Python Sets

Python Sets

  • Sets are unordered collection of data items.
  • in output it doesn't maintain set's order.
  • Sets do not contain duplicate items.
  • Set is enclosed within curly brackets {}, where Lists are with [] & Tuples are with ()
  • Sets are unchangeable, so insert(),append(),extend(),count() are unusable.

Example:

info = {"Carla", 19, False, 5.9, 19}
print(info)

Output:

{False, 19, 5.9, 'Carla'} # not in orignal set's order

Here we see that the items of set occur in random order and hence they cannot be accessed using index numbers.

How to create empty set?

  • To create empty set, we can not use '{ }' as it will create a dictionary.
  • so we have to use set( )
	s = set()
	s1 = {}

    print(type(s))
    print(type(s1))
  • Output:
<class 'set'>  # set
<class 'dict'> # dictionary

Accessing set items:

Using a For loop

You can access items of set using a for loop.

Example:

info = {"Carla", 19, False, 5.9}
for item in info:
    print(item)

Output:

False
Carla
19
5.9

Joining Sets

  • Sets in python more or less work in the same way as sets in mathematics.
  • We can perform operations like union and intersection on the sets just like in mathematics.

I. union():

The union() prints all items that are present in the two sets.

Union.jpg

  • The union() method returns a new set

Example:


cities = {"Tokyo", "Madrid", "Berlin", "Delhi"}
cities2 = {"Tokyo", "Seoul", "Kabul", "Madrid"}
cities3 = cities.union(cities2)
print(cities3)

Output:
{'Tokyo', 'Madrid', 'Kabul', 'Seoul', 'Berlin', 'Delhi'}

Update():

  • update() method adds item into the existing set from another set.

Example:

cities = {"Tokyo", "Madrid", "Berlin", "Delhi"}
cities2 = {"Tokyo", "Seoul", "Kabul", "Madrid"}
cities.update(cities2) # Updating 'cities' set
print(cities)

Output:

{'Berlin', 'Madrid', 'Tokyo', 'Delhi', 'Kabul', 'Seoul'} 
 

Intersection():

The intersection() prints only items that are similar to both the sets.

Intersection.png

  • The intersection() method returns a new set.

Example:

cities = {"Tokyo", "Madrid", "Berlin", "Delhi"}
cities2 = {"Tokyo", "Seoul", "Kabul", "Madrid"}
cities3 = cities.intersection(cities2)
print(cities3)
Output:
{'Madrid', 'Tokyo'}

intersection_update():

  • intersection_update() method updates into the existing set from another set.

Example :

cities = {"Tokyo", "Madrid", "Berlin", "Delhi"}
cities2 = {"Tokyo", "Seoul", "Kabul", "Madrid"}
cities.intersection_update(cities2)
print(cities)
Output:
{'Tokyo', 'Madrid'}

symmetric_difference():

  • The symmetric_difference() prints only items that are not similar to both the sets & returns a new set.

Example:

cities = {"Tokyo", "Madrid", "Berlin", "Delhi"}
cities2 = {"Tokyo", "Seoul", "Kabul", "Madrid"}
cities3 = cities.symmetric_difference(cities2)
print(cities3)

Output:

{'Seoul', 'Kabul', 'Berlin', 'Delhi'}

symmetric_difference_update():

  • symmetric_difference_update() method updates into the existing set from another set.

Example:

cities = {"Tokyo", "Madrid", "Berlin", "Delhi"}
cities2 = {"Tokyo", "Seoul", "Kabul", "Madrid"}
cities.symmetric_difference_update(cities2)
print(cities)

Output:

{'Kabul', 'Delhi', 'Berlin', 'Seoul'}

difference():

  • The difference() prints only items that are only present in the original set and not in both the sets.
  • This is setA - setB
  • The difference() method returns a new set