Sets in Python

What are Sets in Python?

A set is an unordered container of hashable items. You will become familiar with hashable items later, for the time being, remember that only immutable data types can be elements of a set. Because of the structure, sets do not record order of insertion or element position, so you can’t retrieve an element by its index.

How to Create sets in Python

At first, we create a set by listing its elements in curly braces. The only exception would be an empty set that can be formed with the help of a set()function:

my_set = set()
print(type(my_set))
 
my_dict = {}
print(type(my_dict))  

#output
#<class 'set'>
#<class 'dict'>


If you pass a string or a list into set(), the function will return a set consisting of all the elements of this string/list:

dogs = {'labrador', 'lhasa apso', 'akita', 'chow chow', 'beagle'}
 
print(dogs) #the order is not preserved
 
letters = set('mississippi')
print(letters)

#output
#{'chow chow', 'labrador', 'akita', 'lhasa apso', 'beagle'}
#{'i', 'p', 'm', 's'}

Each element is considered a part of a set only once, so double letters are counted as one element:

let = set('Welcome')
print(len(let))
print(let)

#output
#6
#{'o', 'e', 'c', 'm', 'l', 'W'}

Moreover, using sets can help you avoid repetitions:

states = ['Punjab', 'Karnataka', 'West Bengal', 'Gujarat', 'West Bengal']
print(set(states)) 

#output
#{'Gujarat', 'West Bengal', 'Punjab', 'Karnataka'}

Have a look: as the order of naming the elements doesn’t play any role, the following two sets will be equal

set1 = {'A', 'B', 'C'}
set2 = {'B', 'C', 'A'}
print(set1 == set2) 

#output
#True

Working with a set’s element

You can:

  • get the number of set’s elements with the assistance of len() function.
  • iterate all the elements utilizing for loop.
for let in set("welcome"):
    print(let)

#output
#o
#w
#e
#c
#m
#l
  • check whether an element belongs to a particular set or not (in/not in operators), you get the boolean value.
nums = {1, 2, 2, 3}
print(1 in nums, 4 not in nums)

#output
#True True
  • add a new element to the set with add() method or update() it with another collection
nums = {1, 2, 2, 3}
nums.add(5)
print(nums)

#output
#{1, 2, 3, 5}
  • delete an element from a specific set using discard/remove methods. The only difference between them operating is a situation when the deleted element is absent from this set. In this case, discard does nothing and remove generates a KeyError exception.
nums.remove(2)
print(nums)
 
 
empty_set = set()
empty_set.remove(2)
print(empty_set) 

#output
#{1, 3, 5}
#KeyError: 2
  • remove one random element using pop() method. As it’s going to be random, you don’t need to choose an argument.
nums = {1, 2, 2, 3}
nums.pop()
print(nums)

#output
#{2, 3}
  • delete all elements from the set with clear() method.

Set operation

One of the principal features of sets is that they permit you to perform mathematical set operations, for example, intersection and union. That is, if you have at least two sets, you can utilize special methods to see which objects are contained inside each of these sets, or objects present in one of them however not in the another, or get the total sets of objects contained in each set nearby:

Union

Before we start, it must be said that you can perform each set operation in two different ways: by utilizing an operator or by calling a method. In this way, first, let’s see how it functions with the most simple operation — union. At the point when you perform a union on two sets, you get another set that includes all the elements that were inside the united sets. The set method for this is (clearly) referred to as union, and it ought to be called as a method for one of your sets that accepts another set as an argument: A.union(B). Here is a model:

continent_1 = {'Asia', 'Africa', 'Europe','North America'}
continent_2 = {'South America', 'Antartica', 'Australia'}
tot_continents = continent_1.union(continent_2)
print(tot_continents)

#output
#{'Africa', 'Australia', 'North America', 'South America', 'Antartica', 'Asia', 'Europe'}


You can do the same by means of the operator |, the syntax for which is more straightforward. You simply put the operator between your sets, just like that: A | B.

continent_1 = {'Asia', 'Africa', 'Europe','North America'}
continent_2 = {'South America', 'Antarctica', 'Australia'}

tot_continents = continent_1 | continent_2

also_continents = continent_1.union(continent_2)
# let's compare
print(tot_continents == also_continents)

#output
#True

You can also unite sets without creating a whole new set — just by adding all the element from one set to another one. The operator for that is |= (A |= B) and the method is called update. Check this out:

ghostbusters = {'Peter', 'Raymond', 'Egon'}
soldiers = {'Winston'}
secretaries = {'Janine'}
 
ghostbusters |= soldiers
ghostbusters.update(secretaries)
print(ghostbusters)

#output
#{'Peter', 'Egon', 'Raymond', 'Winston', 'Janine'}

Intersection

The intersection permits you to get just the objects that are available in each set. So, by calling the method intersection or by utilizing the operator & in a similar way as of union, you can get what your sets have in common.

places = {'Delhi', 'Bangalore', 'Kolkata', 'Pune', "Noida"}
capitals = {'Bangalore', 'Mumbai', 'Kolkata'}
tot_places = places.intersection(capitals)
print(tot_places)
print(places & capitals)

#output
#{'Kolkata', 'Bangalore'}
#{'Kolkata', 'Bangalore'}

To delete from the first set all the elements that are absent in the second set, and leave only the elements that both sets contain, you can use the operator &= or the method intersection_update.

creatures = {'human', 'rabbit', 'cat'}
pets = {'rabbit', 'cat'}
creatures.intersection_update(pets)
print(creatures)
beasts = {'crocodile', 'cat'}
creatures &= beasts
print(creatures)

#output
#{'rabbit', 'cat'}
#{'cat'}

Difference

Difference operation is equivalent to the simple subtraction of sets: subsequently, you’ll get the set containing all the unique elements of the first set. The name of the method, difference, is the – operator.

places = {'Delhi', 'Bangalore', 'Kolkata', 'Pune', "Noida"}
capitals = {'Bangalore', 'Mumbai', 'Kolkata'}
tot_places = places.difference(capitals)
print(tot_places)
print(places - capitals)

#output
#{'Pune', 'Noida', 'Delhi'}
#{'Pune', 'Noida', 'Delhi'}

Similarly to previous operations, to remove from your set all the elements present in the second set without creating a new collection, you can address the difference_update method or the operator -=.

criminals = {'Al Capone', 'Blackbeard', 'Bonnie and Clyde'}
gangsters = {'Al Capone'}
pirates = {'Blackbeard'}
 
criminals.difference_update(gangsters)
criminals -= pirates
print(criminals)

#output
#{'Bonnie and Clyde'}

Built-in functions with Set

Built-in functions like all(), any(), enumerate(), len(), max(), min(), sorted(), sum() etc. are commonly used with sets to perform different tasks.

  • all()  –  Returns True if all elements of the set are true (or if the set is empty).
  • any()  –  Returns True if any element of the set is true. If the set is empty, returns False.
  • enumerate()  –  Returns an enumerate object. It contains the index and value for all the items of the set as a pair.
  • len()  –  Returns the length (the number of items) in the set.
  • max()  –  Returns the largest item in the set.
  • min()  –  Returns the smallest item in the set.
  • sorted()  –  Returns a new sorted list from elements in the set(does not sort the set itself).
  • sum()  –  Returns the sum of all elements in the set

Now, let’s try to solve an example:

You are given several sets with names of students in different classes. Output the set containing names of all the students.

Sample Input 1:

Potter Weasley
Lovegood Corner
Malfoy Goyle
Bones Macmillan
Sample Output 1:

{‘Potter’, ‘Weasley’, ‘Lovegood’, ‘Corner’, ‘Malfoy’, ‘Goyle’, ‘Bones’, ‘Macmillan’}

gryffindor = set(input().split())
ravenclaw = set(input().split())
slytherin = set(input().split())
hufflepuff = set(input().split())
houses = [gryffindor, ravenclaw, slytherin, hufflepuff]
print(set.union(*houses))

Output