## What is Set in Python?

Sets are the data types in python that are used to store the collection of data. These data can be of any type. It can be an integer, float, string, or both. Sets are similar to List and Tuples. In Sets, values are stored inside the curly brackets. Values stored in the sets cannot be duplicated. They are unique. Even if we define any duplicate value, python will ignore it while executing the sets. We have various inbuilt set functions that can be used to perform various operations on Sets.

### How Sets Work in Python?

Commas separate elements that are stored inside the curly brackets. In the list, we can have another list inside a list or a nested list. But in Sets, we cannot store a list or set or dictionary as an element of the set. The values stored in the sets are mutable means they can be changed.

If we want to search for any value in the set using index value, it will give an error. Sets are unordered means values don’t have fix position. So we cannot perform any kind of operation using an index. We cannot create an empty set by just initializing empty curly brackets; it will create a dictionary. To create an empty set, we have to use the set function. If you want to list functions supported by sets, you can use the ‘dir’ method and pass the set inside it. It will return the list of methods supported by sets.

An example of a python set function is given below:

**Code:**

```
sample = {1,2,3,4,5,6}
print(sample)
```

**Output:**

As I mentioned earlier, the doesn’t take duplicate values.

**Code:**

```
sample = {1,2,3,4,5,6,5}
print(sample)
```

**Output:**

5 is appearing 2 times, but python will consider the value which appeared first and skips the next value while executing the set.

Suppose you have a List and consists of many duplicates values, then we can use sets to remove those duplicate values.

**Code:**

```
sample = {1,2,3,3,4,4,5}
print(sample)
```

**Output:**

### Implementing Set Function in Python

Methods to implement a set function in python is given below:

#### 1. Add Method

This method is used to add the element to the set. The element will be added to the unspecific location as it is unordered.

**Code:**

```
sample = {1,2,3,4,5,6,5}
sample.add(7)
print(sample)
```

**Output:**

The sample added cannot be added again in the set.

#### 2. Remove Method

This method is used to remove any value from the list as there is no indexing, so we have to pass the value that we want to remove from the set.

**Code:**

```
sample = {1,2,3,4,5}
sample.remove(5)
print(sample)
```

**Output:**

#### 3. Discard Method

This method is also used to remove an element from the set, but the remove method will generate an error if the passed value does not exist in the set. This method is used when we don’t know the value that we are passing exist or not. This method won’t return any error.

**Code:**

```
sample = {1,2,3,4,5}
sample.discard(5)
print(sample)
```

**Output:**

#### 4. Clear Method

This method is used to remove all the elements from the set.

**Code:**

```
sample = {1,2,3,4,5}
sample.clear()
print(sample)
```

**Output:**

#### 5. Copy Method

This method is used to make a copy of the Set.

**Code:**

```
sample = {1,2,3,4,5}
new_sample = sample.copy()
print(new_sample)
```

**Output:**

#### 6. Pop Method

This method is used to remove an element from the set. If we don’t specify any element in the pop method, then by default, it will remove the first element of the set.

**Code:**

```
sample = {1,2,3,4,5}
sample = sample.pop()
print(sample)
```

**Output:**

#### 7. Update Method

We can use this method to add multiple values to a set, we can also pass new sets or list or both, and it will be added to the set.

**Code:**

```
sample = {1,2,3,4,5}
sample.update([6,7,8])
print(sample)
```

**Output:**

**Code:**

```
sample = {1,2,3,4,5}
sample.update([6,7,8])
new_set = {5,9,10,11}
sample.update([1,6,7,8],new_set)
print(sample)
```

**Output:**

So as you can first, we have added that was having duplicate values that already existed inside the set, and we have passed a new set that also had duplicate value. But when we print to set, it discards all duplicate values.

### Python Set Operation

We can perform mathematical operations like Union, Intersection, Difference on sets. This can be done in two ways using methods or operators.

#### 1. Union

This function used to merge the elements of two sets into one set.

**Code:**

```
sample = {1,2,3,4,5}
new_sample = {6,7,8}
sample_union = (sample | new_sample)
print(sample_union)
```

**Output:**

**Code:**

```
sample = {1,2,3,4,5}
new_sample = {6,7,8}
sample_union = sample.union(new_sample)
print(sample_union)
```

**Output:**

#### 2. Intersection

This method is used to find out the common elements from two sets.

**Code:**

```
sample = {1,2,3,4,5}
new_sample = {1,2,3}
sample_intersection = (sample & new_sample)
print(sample_intersection)
```

**Output:**

**Code:**

```
sample = {1,2,3,4,5}
new_sample = {1,2,3}
sample_intersection = sample.intersection(new_sample)
print(sample_intersection)
```

**Output:**

#### 3. Difference

This method is used to find the difference of the element from one set to the second set. It means it will return the element that doesn’t exist in the set first as compared to the set second.

**Code:**

```
sample = {1,2,3,4,5}
new_sample = {4,5,6,7,8,9}
sample_difference = (sample - new_sample)
print(sample_difference)
```

**Output:**

**Code:**

```
sample = {1,2,3,4,5}
new_sample = {4,5,6,7,8,9}
sample_difference = sample.difference(new_sample)
print(sample_difference)
```

**Output:**

### Conclusion

So as we have seen, Set is a very useful data type in Python. It is very similar to the list, the only difference is brackets, and it doesn’t take duplicates values. We can use sets to store the data where the data has to be unique is very important, and ordering of the data is not important as they are not unordered.

### Recommended Articles

This is a guide to Python Set Function. Here we discuss the basic concept, methods to implement a set function in python, and different examples and code implementation. You may also look at the following articles to learn more –