Learn Python: Sets
Tags: python, sets, tutorial, beginners
Published: 2020-05-22 21:29:53

---
title: Learn Python: Sets
published: true
description: Introduction to sets in Python
tags: python, sets, tutorial, beginners
series: [Learn Python]
cover_image: https://images.pexels.com/photos/4050296/pexels-photo-4050296.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=650&w=940
---

A set is another type of collection like `list` and `tuple`.

So what are sets?
- Sets are unordered – *Items stored in a set aren’t kept in any particular order*.
- Set items are unique – *Duplicate items are not allowed*.
- Sets are unindexed – *You cannot access set items by referring to an index*.
- Sets are changeable (mutable) – *They can be changed in place, can grow and shrink on demand*.


The values of a set are defined within curly braces `{` `}`.
A quick reminder:
- For `list`, we use square brackets `[` `]`.
- For `tuple`, we use brackets `(`  `) `.

```py
fruits = {'apple','orange'}
print(fruits);
```

![](https://i.imgur.com/JV26K8O.png)


Sets don't maintain order. Every time we execute the code, the order of the values in the set changes.

Do note, in the example below we are adding `pineapple` multi times, but when we print the set, we only see `pineapple` once. This is because set does not allow duplicate.

```py
fruits = {'apple','orange'}
fruits.add('pineapple');
fruits.add('pineapple');
fruits.add('pineapple');
print(fruits);
```
![](https://i.imgur.com/tW0mMBj.png)
![](https://i.imgur.com/47xfyHL.png)
![](https://i.imgur.com/bqOuXtO.png)

 
## Benefits of sets
The key benefit of `sets` is that they make comparing values between two sets easier.


## Set Operations
Sets are commonly used for computing mathematical operations such as intersection, union, difference, and symmetric difference.
![](https://i.imgur.com/xg034qG.png)

### Difference  
```py
basket_1 = {"Strawberry", "Cherry", "Apple", "Grapes"}
basket_2 = {"Pear", "Avocado", "Lime", "Apple", "Grapes"}

# Difference  
basket_1_difference = basket_1.difference(basket_2);
basket_2_difference = basket_2.difference(basket_1);

print(basket_1_difference);
print(basket_2_difference);
```
![](https://i.imgur.com/TSyFnnM.png)


### Symmetric Difference   
```py
basket_1 = {"Strawberry", "Cherry", "Apple", "Grapes"}
basket_2 = {"Pear", "Avocado", "Lime", "Apple", "Grapes"}

# Symmetric Difference 
basket_1_symmetric_difference = basket_1.symmetric_difference(basket_2);
basket_2_symmetric_difference = basket_2.symmetric_difference(basket_1);
print(basket_1_symmetric_difference);
print(basket_2_symmetric_difference);
```
![](https://i.imgur.com/TMDg71g.png)


### Intersection

```py
basket_1 = {"Strawberry", "Cherry", "Apple", "Grapes"}
basket_2 = {"Pear", "Avocado", "Lime", "Apple", "Grapes"}

# Intersection
basket_1_intersection = basket_1.intersection(basket_2);
print(basket_1_intersection);
```
![](https://i.imgur.com/O4Ivtt2.png)

### Union
```py
basket_1 = {"Strawberry", "Cherry", "Apple", "Grapes"}
basket_2 = {"Pear", "Avocado", "Lime", "Apple", "Grapes"}

# Union
basket_1_union = basket_1.union(basket_2);
print(basket_1_union);
```
![](https://i.imgur.com/hcYOmZi.png)

 

<hr>
{% replit @rishiabee/learnpysets%}