# What Data Structure Are Sets in Python?

//

Scott Campbell

A set is a built-in data structure in Python that represents an unordered collection of unique elements. In other words, it is a collection that contains no duplicate items. Sets are commonly used when we want to store a group of items without any specific order or when the uniqueness of the elements is important.

## Creating a Set

To create a set in Python, you can use curly braces {} or the built-in set() function. Let’s see some examples:

Example 1:
“`python
fruits = {‘apple’, ‘banana’, ‘orange’}
“`

Example 2:
“`python
fruits = set([‘apple’, ‘banana’, ‘orange’])
“`

In both examples, we created a set named “fruits” containing three elements: apple, banana, and orange.

## Basic Operations on Sets

Sets in Python support various operations such as adding elements, removing elements, checking membership, and performing mathematical operations like union and intersection.

To add an element to a set, you can use the add() method.

Example:
“`python
“`
After executing this code, the “fruits” set will contain mango as well.

### Removing Elements

To remove an element from a set, you can use the remove() method or the discard() method. The only difference between these two methods is that remove() raises a KeyError if the element doesn’t exist in the set, while discard() doesn’t raise any error.remove(‘banana’)
“`
After executing this code, the “fruits” set will no longer contain the element banana.

### Checking Membership

To check if an element exists in a set, you can use the in keyword.

Example:
“`python
if ‘apple’ in fruits:
print(‘Apple is in the set’)
“`

## Set Operations

Sets in Python also support mathematical operations like union, intersection, difference, and symmetric difference.

### Union

The union of two sets A and B is a set that contains all the elements from both sets without any duplicates. In Python, you can use the union() method or the | operator to perform the union operation on sets.

Example:
“`python
set1 = {1, 2, 3}
set2 = {2, 3, 4}
union_set = set1.union(set2)
# OR
union_set = set1 | set2
“`
After executing this code, the “union_set” will contain {1, 2, 3, 4}.

### Intersection

The intersection of two sets A and B is a set that contains only the common elements between both sets. In Python, you can use the intersection() method or the & operator to perform the intersection operation on sets.

Example:
“`python
set1 = {1, 2, 3}
set2 = {2, 3, 4}
intersection_set = set1.intersection(set2)
# OR
intersection_set = set1 & set2
“`
After executing this code, the “intersection_set” will contain {2, 3}.

### Difference

The difference between two sets A and B is a set that contains the elements present in A but not in B. In Python, you can use the difference() method or the operator to perform the difference operation on sets.

Example:
“`python
set1 = {1, 2, 3}
set2 = {2, 3, 4}
difference_set = set1.difference(set2)
# OR
difference_set = set1 – set2
“`
After executing this code, the “difference_set” will contain {1}.

### Symmetric Difference

The symmetric difference between two sets A and B is a set that contains the elements present in either A or B but not both. In Python, you can use the symmetric_difference() method or the ^ operator to perform the symmetric difference operation on sets.

Example:
“`python
set1 = {1, 2, 3}
set2 = {2, 3, 4}
symmetric_difference_set = set1.symmetric_difference(set2)
# OR
symmetric_difference_set = set1 ^ set2
“`
After executing this code, the “symmetric_difference_set” will contain {1, 4}.

## Conclusion

Sets in Python are a powerful data structure when you need to store a collection of unique elements. They offer efficient operations for adding, removing, and checking membership of elements.

Additionally, sets support mathematical operations like union, intersection, difference, and symmetric difference. By using sets effectively in your Python programs, you can simplify your code and improve its performance.