# Learn Python: Sets

### Rishi ・2 min read

Learn Python (8 Part Series)

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`(`

`)`

.

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

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.

```
fruits = {'apple','orange'}
fruits.add('pineapple');
fruits.add('pineapple');
fruits.add('pineapple');
print(fruits);
```

## 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.

### Difference

```
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);
```

### Symmetric Difference

```
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);
```

### Intersection

```
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);
```

### Union

```
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);
```

Learn Python (8 Part Series)

Sets are one of my favorite container types. You can do really powerful operations with very little effort, and they are wicked fast at it.

The one thing that I see your missing is the set operators like + * - | ^

Waylon, Thanks!

Will definitely include that in another article about advance features of sets.

Much appreciated. Thanks.

As much as I like the tersness of the operators, and being able to quickly compare datasets ad-hoc, using the method names can be much more readable and less confusing. Good call to use those. I guess its worth a mention because you will run into them into the wild.

Nice article, Rishi! Great notes. Thanks for sharing them.

I would add to the benefits of sets that testing for membership in Python using sets if significantly faster than doing it with lists or tuples.

If you have a list of values "a" and want to test if it is a member of another list "b," the test runs significantly faster if "b" is a set.

For the benefit of other readers, here is a Stack Overflow answer explaining why searching for a value in a set is faster than searching in a list.

Javier,

Thank for your input, much appreciated.

I'm taking note, I'll definitely add that when I review it.

Again, thanks!

Love the use of replit here