A set in Python is an unordered collection of unique items. Like a dictionary in Python, there is no indexing of set elements. The sets are mutable which means we can add or remove elements from them.

Python sets can also be used to perform mathematical set operations such as union, intersection, set difference, etc.

In this article, you will learn about Python sets and different operations that can be performed on them.

## How to create a set in Python

You can create a Python set in two ways first is by placing comma-separated elements inside the curly braces and the second is to use the set() function.

For example –

s = {2,5,9,4,1,5}

Or to create using set() use –

s1 = set([6,8,4,4,5,9])

Now when you print the set it will remove duplicate items there if any and get printed.

## Adding elements to a set

There are two methods that you can use to add the elements to a set. These methods are given below.

### Using add() method

You can add an item to a set using add() method.

s = {2,5,9,4,1,5}

to add 3 in this set we will use –

s.add(3)

Let us see the output in the given image –

### Using update() method

By using the update() method you can add multiple items to a set. It takes tuples, lists, strings, or other sets as its argument.

For example –

s = {2,5,9,4,1,5}

to add 3,6,7,8 we will use –

s.update([3,6,7,8])

Now when you print this set you will see –

## Removing elements from a set

There are different methods that can be used to remove elements from a set. The remove() method removes the element that you passed as the argument if elements not present in the set it will raise an error.

s = {2,5,9,4,1,5}

To remove 9 from this set we will use –

s.remove(9)

Removing an element that is not present will raise an error –

s.remove(6)

You can see this in the given image –

Similarly, you can use the discard() method to remove an element from a set. If the element is not present this will leave the set unchanged, like the remove() method this will not raise any error. An example of using the discard() method is –

s.discard(1)

Another method that you can use to remove an element from a set is the pop() method. This method randomly selects and deletes an item from the set.

For example –

s.pop()

Now if you want to remove all the elements of a set then use the clear() method. For example –

s.clear()

## Looping in a set

You can use for loop to iterate over a set. Lets we have the set s.

s = {2,5,9,4,1,5} # to iterate and print the set items use for items in s: print(i)

This will print all the elements of set s.

## Python set operations

On Python sets, you can perform mathematical set operations such as union, intersection, set difference, etc. You can use operators or methods provided in python.

Suppose we have two sets i.e. x and y we will perform set operations on them.

x={1,9,6,7,0,3} y={5,1,9,4,7,2}

### Set union operation

The union of two sets is the set of elements that are in both sets, common elements of both sets will be written once.

You can use one of the following statements to find the union of two sets.

# Use | operator to find the union of x and y print (x|y) # OR use union() method to find the union of x and y print (x.union(y))

You can see the output in the given image –

### Set intersection operation

The intersection of two sets is the set of elements that are common in both sets.

You can use one of the following statements to find the intersection of two sets.

# Use & operator to find the intersection of x and y print (x|y) # OR use intersection() method to find the intersection of x and y print (x.intersection(y))

You can see the output in the given image –

### Set difference

The x difference y is the set of elements that are present in set x and not in y. Similarly, y difference x is the set of elements that are present in set y and not in x.

When you execute the following statements you will see the difference of x and y, y and x –

# The given statement will print x difference y print(x-y) # Following statement will print y difference x print(y-x)

The given image shows the output of the given program –

### Symmetric difference

The symmetric difference of x and y is the set of elements that are in x and y and not in both (excluding intersection of set x and y). You can use the ^ operator or symmetric_difference() method to find the symmetric difference of two sets.

For example –

# Display symmetric difference of x and y using ^ operator print(x^y) # OR use symmetric_difference() method print(x.symmetric_difference(y))

See the output of this in the given image –

You can find some more methods and use with python sets here.

Now if you have a query on this topic then write us in the comments below.