# Sets in Python

• A set is an iterable unordered collection of data types.
• The set is a mutable data type which means we can change it after its creation.
• The set contains unique elements, we can’t store duplicate elements in it.
• We can store only immutable data types inside a set.
• The set doesn’t support indexing which means we can not access the elements of the set directly.
• We can create a set by putting the element inside in `{}` (curly braces).

Example – creating a set in Python

```				```
# Example 1-
s={1,2,3,(10,11,12),'anurag','edSlash'}
print(s)
print(type(s))

# Output
# {1,2,3,(10,11,12),'anurag','edSlash'}
# <class 'set'>

# Example 2-
st={1,2,2,3,4,4,4} # it will remove the extra 2,4 from set
print(st)

# Output
# {1,2,3,4}

# Example 3 -
t={'hey','Python',[12,13,17]} # Will give an error
print(t)

# Error -
# Traceback (most recent call last):
# File "/home/main.py", line 1, in <module>
# t={'hey','Python',[12,13,17]} # Will give an error
# TypeError: unhashable type: 'list'
```
```

Creating a set using set method –

• In Python, there is a function `set()` which is also used to create a set.

Example

```				```
sets=set([1,2,3,4])
print(sets)
print(type(sets))

# Output
# {1,2,3,4}
# <class 'set'>

# Create an empty set
n=set()
print(n)
print(type(n))

# Output
# set()
# <class 'set'>
```
```

#### How to add an element to a set?

• In a Python set, we can add an element in a set using `.add()` method.
• It only adds a single element at one time.

Example –

```				```
# Example 1-
s={1,2,3,4}
print(s)
print(s)

# Output -
# {1,2,3,4}
# {1,2,3,4,5}

# Example 2-
s={'hello','Everyone'}
print(s)
print(s)

# Output -
# {'hello','Everyone'}
# {'hello','Everyone','edSlash'}
```
```

#### How to Update a Set?

• We can update any set using `.update()` method.
• We can update a set using any collection data type.

Example – let’s see an example

```				```
st={'hello','Python','set'}
print(st)
l=['This','is','edSlash','Official']
st.update(l)
print(st)

# Output -
# {'hello','Python','set'}
# {'hello','Python','set','This','is' 'edSlash','Official'}
```
```

#### Deleting an element from a set –

• We have two ways to delete an element from a set.
1. `.discard()` method –
• Using the discard method we can delete an element from a set.
• when an element is not present in a set it will not give an error.

Example –

```				```
s={10,20,30,40}
print(s)
print(s)

# Output -
# {10,20,30,40} #original set
# {10,20,40}    # after deleting 30 from set
```
```

`.remove()` method –

• We can also delete an element from a set using the remove method.
• if an element is not present in a set it will raise an error.

Example –

```				```
# Example 1 -
s={10,20,23,(1,2,3)}
print(s)
s.remove((1,2,3))
print(s)

# Output -
# {10,20,23,(1,2,3)} # original set
# {10,20,23}         # after removing tuple from set(s)

# Example 2 -
s={'Mr.','Anurag','Dubey'}
print(s)
s.remove('edSlash')  # Will raise an error
print(s)

# Output -
# {'Mr.','Anurag','Dubey'}
# Traceback (most recent call last):
# File "/home/main.py", line 3, in <module>
# s.remove('edSlash')  #Will raise an error
# KeyError: 'edSlash'
```
```

#### How to delete all elements from a set –

• For deleting all elements from a set we have an inbuilt method `.clear()`.
• clear method deletes all elements from a set.

Example –

```				```
s={11,22,33,44,55}
print(s)
s.clear()
print(s)

# Output -
# {11,22,33,44,55} # original set
# set()            # set after deleting all elements
```
```

Delete set from memory –

• We can delete the entire set from memory using `del` keyword

Example – Let’s see an example

```				```
s={1,2,3,4}
print(s)
del s
print(s) # will give an error

# Output -
# {1,2,3,4}
# Traceback (most recent call last):
# File "/home/main.py", line 4, in <module>
# print(s)
# NameError: name 's' is not defined
```
```

#### How do you access elements of a set?

• We can access elements of a set by iterating over a set using a for loop.

Example – let’s see an example

```				```
s={10,20,30,40}
for i in s:
print(i)
# Output -
# 40
# 10
# 20
# 30
```
```
• In the above example, we see that the element access is not sequential. Let’s see what’s the reason behind this behavior of set.
• set is stored data in memory using a hash table and data stored is unordered.
• Data is stored on the basis of their hash values so the ordering of data is not preserved.

#### Set Operations –

• There are various operations that we can do on set.

#### Union in set –

• We can combine two sets using a union operation that contains all unique elements from both sets.
• It removes duplicate elements while merging.
• Union operation doesn’t modify the original set while it returns a new set that contains an element that is present in either the first set or the second set.
• `.union()` method and `|` operator are used to perform union operation on two sets.

Example – let’s see an example

```				```
# Example 1 -
a={1,2,3}
b={3,4,5}
c=a.union(b)
print(c)

# Output -
# {1,2,3,4,5}

# Example 2 -
s1={'a','b','c','d'}
s2={'c','d','e','f','g'}
s=s1|s2
print(s)

# Output -
# {'a','b','c','d','e','f','g'}
```
```

#### Intersection in set –

• We can find common elements between two or more sets using an intersection operation.
• intersection return elements that are present in all sets.
• `&` operator and `.intersection` method is used to perform intersection operations between sets.

Example –

```				```
# Example 1-
set1={1,2,3,4}
set2={3,4,5,6}
final_set=set1&set2
print(final_set)

# Output -
# {3, 4}

# Example 2-
s1={'a','b','c','d'}
s2={'c','d','e'}
s3={'c','b','d'}
s=s1.intersection(s2,s3)
print(s)

# Output -
# {'c', 'd'}
```
```

#### Difference in set –

• It returns a new set that contains elements that are present in one set not in other sets.
• difference operation is used to find unique elements of one set.
• `-` and `.difference` is used to perform difference operations on sets.

Example –

```				```
# Example 1 -
s1={1,2,3,4}
s2={4,5,6,7,8}
s=s1-s2
print(s)

# Output -
# {1, 2, 3}

# Example 2 -
s1={'a','b','c','d'}
s2={'c','d','e'}
s3={'c','b','d'}
s=s1.difference(s2,s3)
print(s)

# Output -
# {'a'}
```
```

#### Why does not set store data in order, Why does the set not store mutable data, and why set not stores duplicate data?

• the set is used as hash table or hash set in the backend to store data in memory.
• hash table uses a hashing function to store data, A hashing function takes an element as an input and produces a certain numerical value called a hash code or hash value for each element of a set.
• this hash code is used as an index for the elements of a set and each element has a unique hash value.
• This is the reason that the data stored is unordered.
• When you attempt to add an element to a set, the set checks whether an element with the same hash code already exists in the set If yes then it ignores the element that’s why we can not store duplicate data in it.
• when we try to store mutable data types inside a set, we all know that we can change the value of any mutable data type and when we modify any mutable data type their hash value will change but we can not change the hash value of an element. That’s the reason we can’t store mutable data in a set.