# Python Set

A Set is an unordered collection data type that is iterable, mutable, and has no duplicate elements.

Python's set class represents the mathematical notion of a set. The major advantage of using a set, as opposed to a list, is that it has a highly optimized method for checking whether a specific element is contained in the set. This is based on a data structure known as a hash table.

A set is a collection which is unordered and unindexed. In Python sets are written with curly brackets.

In a set we can't have duplicate items.

## Creating set

In Python sets are written with curly brackets.

## Accessing values in set

As set is unindexed we can't use `[]` to access elements of a set.

But we can use for loop to access set elements:

## Updating set

You can add to elements in a set with the `add()` method:

## Delete set Elements

Set elements can be removed using `pop()` and `remove()` methods:

• `pop()` method delete element from begining of set
• `remove()` method takes an argument and delete that element from set

## Python Built-in set Functions

Function Description
`len(set)` Gives the total length of the set (duplicate values excluded).
`max(set)` Returns item from the set with max value.
`min(set)` Returns item from the set with min value.
`set(seq)` Converts a tuple or list into set.

## Python Built-in set Methods

Method Description
`set.add(x)` Adds the item x to set if it is not already present in the set
`set.clear()` Empties the whole set
`set.copy(o)` Copy a set to another set
`set.union(s)` Returns a union of two set. Using the `|` operator between 2 sets is the same as writing set1.union(set2)
`set.intersection(s)` Returns intersections/common of two sets.The `&` operator comes can also be used in this case.
`set.difference(s)` Returns a set containing all the elements of invoking set but not of the second set
`set.pop()` Remove element from begining
`set.remove(elem)` Remove elem from set