top of page

Python Sets

Sets are unordered data type and doesn't allow duplicate values.

Create a Set

a = {'Red', 'Green', 'Blue'}

print(a)

Sets cannot contain duplicate values.

a = {'Red', 'Green', 'Blue', 'Red', 'Yellow'}

print(a)

Sets are unordered and cannot be access using the index number.

a = {'Red', 'Green', 'Blue', 'Red', 'Yellow'}

print(a)

print(a[1])

Change Sets Elements

Sets cannot access through index so we even can't change his elements.

So set elements are immutable and we cannot change them.

Change green to black.

a = {'Red', 'Green', 'Blue'}

a[1] = 'black'

print(a)

Even if we can't change the value of an element in the set but we can add or remove elements from the set using the add or remove methods.

Add Set Elements

With the help of add method we can add elements to sets.

add()

add helps us to add the element to the set.

Add black

a = {'Red', 'Green', 'Blue'}

a.add('Black')

print(a)

Delete Set Elements

With the help of below methods we can delete elements of a Set.

pop()

As set is unordered we can't pass index number in pop like we did in list and tuple. but we can simply write pop and it will not delete the last element from set like it does in list and tuple because there is nothing like last element in set. so it will delete the random element from set.

Delete the random element from set.

a = {'Red', 'Green', 'Blue'}

a.pop()

print(a)

discard()

We can delete the particular element by using the discard. Discard will not throw an error if the value is to be deleted is not present in a set.

Delete 'Green'

a = {'Red', 'Green', 'Blue'}

a.discard('Green')

print(a)

remove()

We can delete the particular element by using the remove. Remove will throw an error if the value is to be deleted is not present in a set.​

Delete Green

a = {'Red', 'Green', 'Blue'}

a.remove('Green')

print(a)

del a

Delete can help us to delete the complete set.

Delete the Set

a = {'Red', 'Green', 'Blue', 'Black'}

del a

print(a)

clear()

Clear will help us to clear the set and make it empty.

Clear the Set

a = {'Red', 'Green', 'Blue', 'Black'}

a.clear()

print(a)

Create a new Set

When we want to create one new set with the help of other two existing set we can use this four methods provided below.

Below methods will help us to create the set 'c' with the help of two sets 'a' and 'b'.

union()

union() will add two set.

a = {'Red', 'Green', 'Blue'}

b = {'Pink', 'Black', 'Red'}

c = a.union(b)

print(c)

intersection()

intersection() will return the elements present in both set.

a = {'Red', 'Green', 'Blue'}

b = {'Pink', 'Black', 'Red'}

c = a.intersection(b)

print(c)

symmetric_difference()

symmetric_difference() will return the elements which are not present in both set.

a = {'Red', 'Green', 'Blue'}

b = {'Pink', 'Black', 'Red'}

c = a.symmetric_difference(b)

print(c)

difference()

difference() will return the difference of  set 'a' from set 'b'.

a = {'Red', 'Green', 'Blue'}

b = {'Pink', 'Black', 'Red'}

c = a.difference(b)

print(c)

Modify Existing Set

When we want to modify existing set with the help of other set we can use this four methods provided below. In every method 'update' word will be there.

Below methods will help us to modify set 'a' with the help of set 'b'.

update()

update() will add two set.

a = {'Red', 'Green', 'Blue'}

b = {'Pink', 'Black', 'Red'}

a.update(b)

print(a)

intersection_update()

intersection_update() will return the elements present in both set.

a = {'Red', 'Green', 'Blue'}

b = {'Pink', 'Black', 'Red'}

a.intersection_update(b)

print(a)

symmetric_difference_update()

symmetric_difference_update() will return the elements which are not present in both set.

a = {'Red', 'Green', 'Blue'}

b = {'Pink', 'Black', 'Red'}

a.symmetric_difference_update(b)

print(a)

difference_update()

difference_update() will return the difference of  set 'a' from set 'b'.

a = {'Red', 'Green', 'Blue'}

b = {'Pink', 'Black', 'Red'}

a.difference_update(b)

print(a)

issubset()

issubset() will return ture if all the element from a present in b.

a = {'Red', 'Green', 'Blue'}

b = {'Blue', 'Pink', 'Black', 'Red', 'Green'}

c = a.issubset(b)

print(c)

issuperset()

issuperset() will return ture if all the element from a present in b.

a = {'Red', 'Green', 'Blue'}

b = {'Blue', 'Pink', 'Black', 'Red', 'Green'}

c = b.issuperset(a)

print(c)

bottom of page