Python set is an un-ordered and un-indexed collection of elements.

- Every element is unique.
- The set contains elements that are un-ordered.
- No duplicates are allowed.
- The set itself is mutable i.e. one can add/remove items(elements) from it.
- Unlike arrays, wherein the elements are stored in order, the order of elements in a set is not defined.
- The elements in the set are not stored in their order of appearance in the set.

## Creation of Sets in Python

Set can be created by placing all the elements within curly braces {}, separated by a comma. They can also be created by using the built-in functionÂ `set()`

.

The elements can be of different data types, but a Set doesn’t support mutable elements. Sets are unordered, so one can’t be sure of the order of elements in which they will appear.

### Example: Creation of Sets

```
Days=set(["Mon","Tue","Wed","Thu","Fri","Sat","Sun"])
Fruits = {"apple", "banana", "cherry"}
Name=set('Quit')
print(Name)
print(Fruits)
print(Days)
```

**Output**:

{‘u’, ‘Q’, ‘i’, ‘t’}

{‘cherry’, ‘banana’, ‘apple’}

{‘Sun’, ‘Wed’, ‘Mon’, ‘Thu’, ‘Tue’, ‘Sat’, ‘Fri’}

**Recommended Readings:**

## Access elements from the Python Set

Since, sets are un-ordered and un-indexed, one cannot access the elements by referring to its index, unlike arrays.

The elements of the Sets can be accessed by either of the following ways:

- Iterate through the loop of the set items using a
`for`

loop . - Check whether a specified value is present in a set, by using the
`in`

keyword .

### Example: Accessing elements from a Set

```
Fruits = {"apple", "mango", "cherry"}
for a in Fruits:
print(a)
print("banana" in Fruits)
print("mango" in Fruits)
```

**Output**:

mango

cherry

apple

False

True

## Add elements to a Python Set

We can add elements to a set by using `add() `

function. In case, we require to add more elements we need to use `update()`

method to do so.

### Example: Addition of elements to a Set

```
Fruits = {"apple", "mango", "cherry"}
Fruits.add("grapes")
print(Fruits)
Fruits.update(["banana", "orange", "strawberry"])
print(Fruits)
```

**Output**:

{‘cherry’, ‘apple’, ‘mango’, ‘grapes’}

{‘strawberry’, ‘cherry’, ‘apple’, ‘orange’, ‘banana’, ‘mango’, ‘grapes’}

## Removal of elements from a Set

We can delete the items from the Set using either of the following methods:

- By using
`remove()`

method - By using
`discard()`

method - By using
`clear()`

method – deletes all the elements from the Set - By using
`del()`

method – deletes the entire Set

### Example 1: Using remove() method

```
Fruits = {"apple", "grapes", "cherry"}
Fruits.remove("grapes")
print(Fruits)
```

**Output**:

{‘cherry’, ‘apple’}

### Example 2: Using discard() method

```
Fruits = {"apple", "grapes", "cherry"}
Fruits.discard("grapes")
print(Fruits)
```

**Output**:

{‘cherry’, ‘apple’}

### Example 3: Using clear() method

```
Fruits = {"apple", "grapes", "cherry"}
Fruits.clear()
print(Fruits)
```

**Output**:

set()

### Example 4: Using del() method

```
Fruits = {"apple", "grapes", "cherry"}
del Fruits
print(Fruits)
```

**Output**:

```
Traceback (most recent call last):
File "main.py", line 5, in <module>
print(Fruits)
NameError: name 'Fruits' is not defined
```

## Methods in Sets

Method | Description |
---|---|

add() | Adds an element to the set |

clear() | Removes all the elements from the set |

copy() | Returns a copy of the set |

difference() | Returns a set containing the difference between two or more sets |

difference_update() | Removes the items in this set that are also included in another, specified set |

discard() | Remove the specified item |

intersection() | Returns a set, that is the intersection of two other sets |

intersection_update() | Removes the items in this set that are not present in other, specified set(s) |

isdisjoint() | Returns whether two sets have a intersection or not |

issubset() | Returns whether another set contains this set or not |

issuperset() | Returns whether this set contains another set or not |

pop() | Removes an element from the set |

remove() | Removes the specified element |

symmetric_difference() | Returns a set with the symmetric differences of two sets |

symmetric_difference_update() | inserts the symmetric differences from this set and another |

union() | Return a set containing the union of sets |

update() | Update the set with the union of this set and others |

## Set Operations in Python

Sets are used to carry out mathematical functionality set operations such as union, difference, intersection, and symmetric difference.

### Set Union – Inclusion of all elements from both the sets.

Union operation is performed by either of the following methods:

- By using
`|`

operator - By using
`union()`

method

#### Example: Union of Sets

```
X = {1, 2, 3}
Y = {6, 7, 8}
print(X | Y)
print(Y.union(X))
```

**Output**:

{1, 2, 3, 6, 7, 8}

{1, 2, 3, 6, 7, 8}

### Set Intersection – Inclusion of elements that are common to both the sets.

Intersection operation is performed by either of the following methods:

- By using
`&`

operator - By using
`intersection(`

) method

#### Example: Intersection of Sets

```
X = {1, 2, 3}
Y = {3, 2, 8}
print(X & Y)
print(Y.intersection(X))
```

**Output**:

{2, 3}

{2, 3}

### Set Difference – Inclusion of elements from either of the sets.

(A – B) contains the elements that are only in set A but not in set B.

(B – A) contains the elements that are only in set B but not in set A.

Difference operation is performed by either of the following methods:

- By using
`-`

operator - By using
`difference()`

method

#### Example: Difference of Sets

```
X = {1, 2, 3}
Y = {3, 2, 8}
print(X - Y)
print(Y.difference(X))
```

**Output**:

{1}

{8}

### Set Symmetric Difference – Inclusion of elements from both the sets except the common elements of the sets

Symmetric Difference operation is performed by either of the following methods:

- By using
`^`

operator - By using
`symmetric_difference()`

method

#### Example: Symmetric Difference of Sets

```
X = {1, 2, 3, 9, 0}
Y = {3, 2, 8, 7, 5}
print(X ^ Y)
print(Y.symmetric_difference(X))
```

**Output**:

{0, 1, 5, 7, 8, 9}

{0, 1, 5, 7, 8, 9}

## References

- Python Sets
- Python Official Documentation