Sets in Python 🐍
Tags: python
Published: 2020-08-27 05:54:32

## What are sets and why should you care about them?
Python sets are collections of data that remove duplicates. This is mainly done for the purposes of identifying membership, as well as comparing and contrasting the differences between sets. Utilizing the built-in set data type is more efficient than looping over and manipulating lists, arrays, or dictionaries. 

Sets are mutable, meaning that we can add and remove elements from it. The elements within the set are immutable, hashable objects. This is why sets are so efficient; the set elements are converted to its hash value, which allows for less expensive comparison between elements. Using the frozenset() built-in function creates an immutable version of a set object. I won’t be covering it in this post, but you can read more on it [here] (https://www.programiz.com/python-programming/methods/built-in/frozenset).

## Let’s see how to get started with sets and how to use them.
You can create a set with either curly braces or using the set() function on a list, but remember that a pair of empty curly braces creates a dictionary. 
```python
set_a = {‘red’, ‘pink’, ‘yellow’} 
set_a = set([‘red’, ‘pink’, ‘yellow’])
```
### There are several methods that can be used on sets.
Let's practice with these two sets:
``` python
set_a = {'red','pink’, ‘yellow', 'orange'}
set_b = {'blue’, ‘green’, ‘yellow', 'orange'}
```

**.remove()** removes a specified element from a set. If you attempt to remove an element that is not a member of a set, it raises a KeyError. 
``` python
set_a.remove('yellow') 
print(set_a)
# set_a = {'pink', 'orange', 'red'}
set_a.remove('purple')
print(set_a)
# KeyError: 'purple'
```
**.discard()** removes a specified element from a set, similar to the **.remove()** method. The difference with this method is that if it tries to discard an element that is not present in the set, it does not return an error.
``` python
set_a.discard('red') 
print(set_a)
# {'yellow', 'pink', 'orange'}
set_a.discard('purple')
print(set_a)
# {'yellow', 'pink', 'red', 'orange'}
```
**.add()** adds a new element to the set.
``` python
set_a.add('purple')
print(set_a)
# {‘red’, ‘orange’, ‘purple’, ‘yellow’, ‘pink’}
```
**.pop()** removes a random element from the set. It takes no arguments.
``` python
print(set_a.pop())
# red
```
**.union()** returns all the elements in all the sets being used. It does not update the main set.
```python
print(set_a.union(set_b))
# {'red', 'pink', 'orange', 'yellow', 'blue’, ‘green’, ‘yellow'}
print(set_a)
# {'red', 'yellow', 'pink'}
```
**.copy()** allows you to make a copy of a set. There are two ways to do this, depending on if you want to update changes from original set to the copy or keep the original set in-tact.
``` python
new_set = set_a
# If you do it this way, changes made to set_a will show up in new_set
new_set = set_a.copy()
# new_set will remain however set_a was when it was copied.
```
**.clear()** is pretty self-explanatory. This method removes all elements from a set.
``` python
set_a.clear()
print(set_a)
# set()
```
**.intersection()** will return the common elements from the sets in use. The shorthand for this method is `&`.
``` python
print(set_a.intersection(set_b))
print(set_a & set_b)
# {'yellow', 'orange'}
```
**.symmetric_difference()** will return the elements present in either of the sets but not in both. It is known as the union of the sets without the intersection. The shorthand for this method is `^`.
``` python
print(set_a.symmetric_difference(set_b))
print(set_a ^ set_b)
# {'pink', 'blue’, ‘green', 'red'}
```
**.isdisjoint()** returns True if two sets are disjoint sets. Two sets are disjoint when there are no shared elements.
``` python
print(set_a.isdisjoint(set_b))
# False
```
**.issubset()** returns True if all elements of one set are present in another set.
``` python
set_c = {1, 2, 3}
set_d = {1, 2, 3, 4, 5, 6, 7}
print(set_c.issubset(set_d))
# True
print(set_d.issubset(set_c))
# False
```
_____________
Sets are not that complex at all, but it can make your code cleaner and your life easier. Feel free to comment or reach out. ✨