# Python sets – Make a group of want and don’t want data!

To move forward to know more about python sets. First, you have to know why are we using a set?

What’s the need of set to you?

Sets are used for removing the duplicates item in a group. For example, Removing duplicate registration of the same person in the set.

To know more about sets you are going to see some topics of the set. The topics are following:-

∆Access the set

∆Add the set

∆remove set

∆join the sets

∆Keep only the duplicates

∆Keep all but not the duplicates

∆issubset() method

∆isdisjoint() method

In the first section, you are going to see how can you access the set items. Let’s see how can you do?

For example,

Here, you can see that set is not accessing through indexing?

But why the set does not access the item through indexing?

The reason behind this is that set is an unordered collection of data types.

Set is unordered, unchangeable and doesn’t allow duplicate items.

Check out the sections

## Set Items

Set items are unordered, unchangeable and don’t allow duplicates items in the set.

### Set items – Data type

You would say what’s the data type of set?

The data type of set is ‘set’.

What’s the syntax is to find the data type of set?

You can find type of set with the help of the type() method.

**type() method**

For example,

In the first section, you had seen that you can’t access the set items because of Unordered behaviour.

**∆ Add the set **

You can add the set with two method update and add method.

What’s the difference between them?

The difference is an update() method add another set of items to the first set. And add() method add a single item to set.

Let’s see how both will be done?

**add() method**

The add() method add a particular item to the set.

For example,

Here, you can see that 9 is added to any random position because of the unordered behaviour of a set.

**update() method**

The update() method adds another set item to the first set.

Let’s see how can you do?

You have added 2nd set to the first one.

**1. Add any iterable **

If you have to add any list, dictionary or tuple to set how will you do it?

You can do it with the update() method.

Let’s see how can you do?

For example,

Here, you can see that if any iterable add to set become set.

## ∆**Python – Remove Items**

Suppose you have to remove certain items from the set. How will you remove set items?

You can remove set items from following method:-

∆remove() method

∆discard() method

∆clear() method

∆pop() method

**1.remove() method**

Suppose you want to remove a particular item from the set. How will you remove it?

You can remove it from remove(remove value).

For example,

Here you can see that value 5 has removed.

If the given value in the remove() method will not find then an error will raise.

For example,

Here, In the remove method the value 53 not found in the set. So, a key error raised.

**2. discard() method**

The discard() method is the same as the remove() method. It will also remove the particular item.

For example,

The difference comes when you will pass another value then set it to the discard method not raise the error.

The discard() only come to the given set when the value not found.

For example,

Here, you can see that 56 value is not found in set1. So, it will return the given set.

**3.clear() method**

Suppose you want to create a set where you will add data in future but not know.

You want to clear all old groups of items. How will you do it?

You can do it with the clear() method.

The clear() method doesn’t delete the set. It makes an empty set of data.

For example,

**4.pop() method**

Suppose, you want to remove any last element in set.

You can use the pop() method.

The pop() method will remove any item from the set. Because the set has an unordered collection of data types.

For example,

**Python – Loop sets**

Looping means come item in the sequence form.

You can loop through the set items by using a for a loop.

Here, we have a loop-through x in set.

**Python- Join Two sets**

Suppose, you have to join two sets. How will you join?

You can use the following method to join two sets:-

∆ union() method

∆ union_update() method

∆ intersection() method

∆ intersection_update()

method

**1. union() method**

The union() method combine the two sets and creating a new set. In the new set, the elements of both sets are present.

Here, you can see that the element of both set1 and set2 has come.

**2.intersection** **method**

The intersection() method comes only the elements which are present in both sets.

For example,

Here, 100 is common in both sets. So, the output is 100.

**Keep Only the duplicate**

Suppose, you have to remain the elements that are duplicate in both sets and remove unwanted items from the original set.

How will you do it?

You can do it with the following method:-

**1.intersection_update() method**

Here, you can say that intersection() and intersection_update() methods both are producing the same result.

Then why we use the intersection_update() method also?

The intersection() method creating the new set without removing unwanted items.

The intersection_update() method return a common element in both sets by removing the unwanted item from the original set.

It returns the original set, not the new set.

**Keep all but Not the duplicates**

Suppose, you have to find the element which is present in either set but not at the intersection of both sets.

You can find this with the symmetric_difference() method.

**1.symmetric_difference() method**

For example,

Here, you can see that the new set {200,1,9} created.

The common element doesn’t come.

It is useful to find uncommon elements in the set.

**2.symmetric_difference_update() method**

Suppose you want to update the symmetric_differnce method.

How will you update this set?

You can update symmetric_difference() with symmetric_differnce_update() method.

For example,

Here, you can see that the symmetric_differnce_update() method return the none value.

**Python set- isdisjoint() method**

Suppose, you have to find that sets are disjoint or not. In other words two sets are completely different or not.

For example, the set of passed students and the set of failed students.

You can check the complete difference with the isdisjoint() method.

Let’s see how can you do?

If anyone item matches in both sets. It will return a False result.

For example,

**Python set- issubset?**

If you have to check if the set is a subset of another set. This means the element of the set is present in a specified set.

If all elements of the first set are present in a specified set,then it will return **True** value.

For example,

Here, you can see that all elements of x are present in y, so the result is **True.**

If suppose some elements of the set are not present in a specified set.

What you think will happen?

If you guessed the value will be **False**, you are right.

If any value of the set is not present in a specified set, then the return value will be **False**.

For example,

Here, you can see that the 90 value is not present in y. So, x is not a subset of y.

The subset is that one set is part of another set.

The operations which we have performed in the set are very useful in mathematics.

*Conclusion:-*

*Set is unordered, unchangeabl*e *and doesn’t allow for duplicate items. With help of *a *set, you can create* *a group of wanted and unwanted data.*

*Set can’t be accessed*. *Because of the unordered collection of data types*.

*Set has much practical application, for example, finding successful and unsuccessful student*s* in class.*

*You can combine set, foun*d *common elements and create a new set.*

- A new way to format the string in Python 3.6: fstring - October 11, 2021
- Ultimate new guide for python beginners in 2021 - October 3, 2021
- How to download famous thonny easily for python? - September 20, 2021