# Python Sets

In Python, a set is an unordered collection of unique elements. Sets are used to store multiple items in a single variable. Sets are useful when you need to keep track of a collection of elements, but don't care about the order, or when you want to eliminate duplicates.

Elements in a set are unordered.

Elements in a set are unique. A set doesn’t allow duplicate elements.

Elements in a set cannot be changed. For example, they can be numbers, strings, and tuples, but cannot be lists or dictionaries.

#### Creating a set in python

In Python, you can create a set using the `set()`

function or using curly braces `{}`

. Note that using curly braces `{}`

to create a set will only work if you have a list of elements. If you try to create a set from a list using curly braces, you will actually create a dictionary.

To create a set from a list of elements, you should use the set() function or curly braces {}.

#### Creating an empty set

To create an empty set, you should use the set() function.

#### Clear a set

To remove all the items in a set, we can use the `.clear()`

method to achieve this.

#### Remove items from a set

When removing items from a set, there are different ways to achieve this.

**Removing items using ****.remove(key)**

The `.remvoe()`

method removes an item from the set. This operation would throw an error if the item is not found in the set

This is an example:

**Removing items using ****.discard(key)**

When using discard, it checks if the item exists in the set before removing the item

This is an example:

**Removing items using ****.pop()**

In Python, the `set.pop()`

method removes and returns an arbitrary element from the set. If the set is empty, it raises a `KeyError`

.

This is an example:

#### Add items to a set

We can add items to a set using the `.add(value)`

method

This is an example:

#### Set Comprehension

A set comprehension is a concise way to create a new set from an iterable. It is similar to list comprehensions and dictionary comprehensions, but it creates a set instead of a list or dictionary.

Here is the syntax for a set comprehension:

Here is an example of a set comprehension:

Set comprehensions are a concise and efficient way to create sets in Python. They are also a good way to learn about the syntax and usage of comprehensions in Python.

#### Set Union

In Python, you can use the union() method to find the union of two sets. The union of two sets is a new set that contains all the elements from both sets, without any duplicates.

We can also use the `|`

operator to find the union of two sets. This is known as the pipe operator.

Both of these methods will return a new set that contains all the elements from both sets, without any duplicates.

#### Set Intersection

In Python, you can use the intersection() method to find the intersection of two sets. The intersection of two sets is a new set that contains only the elements that are common to both sets.

Here is an example of set intersection in python:

We can also use the `&`

operator to find the intersection of two sets. This is known as the ampersand operator.

#### Set Difference

In Python, you can use the difference() method to find the difference of two sets. The difference of two sets is a new set that contains the elements that are in the first set, but not in the second set.

Here is an example showing a set difference:

You can also use the `-`

operator to find the difference of two sets. This is known as the subtraction operator.

#### Set Symmetric_difference

In Python, you can use the symmetric_difference() method to find the symmetric difference of two sets. The symmetric difference of two sets is a new set that contains the elements that are in one set or the other, but not both.

Here is an example showing a set symmetric_difference:

You can also use the `^`

operator to find the symmetric difference of two sets. This is known as the caret operator.

#### Set subset

In Python, you can use the `issubset()`

method to check if a set is a subset of another set. A set is a subset of another set if all the elements in the first set are also in the second set.

Here is an example showing a set subset:

You can also use the `<=`

operator to check if a set is a subset of another set. This is known as the less than or equal to operator.

#### Set superset

In Python, you can use the `issuperset()`

method to check if a set is a superset of another set. A set is a superset of another set if it contains all the elements in the other set.

Here is an example showing a set superset:

You can also use the >= operator to check if a set is a superset of another set. This is known as the greater than or equal to operator.

#### Set disjoint

In Python, you can use the `isdisjoint()`

method to check if two sets are disjoint. Two sets are disjoint if they have no elements in common.

Here is an example showing a set disjoint:

The isdisjoint() method returns True if the two sets are disjoint, and False otherwise.

Note that the empty set is considered to be disjoint with all other sets.

#### Frozen set

A frozen set is an immutable version of a Python set object. It is a set that cannot be modified after it is created. You can create a frozen set by using the frozenset() function

Frozen sets are useful when you need to use a set as a key in a dictionary or as an element in another set, because immutable objects can be used as keys or elements.

Frozen sets also support all the methods and operations that regular sets do, such as union, intersection, and difference.

Last updated