# What is the set in Python

March 27, 2022

## What is the set in Python

In this article we will learn what is the set in Python and also ways to use it in your projects, what is the use of this collection in the language.

The set is a collection, which represents a set in the Python language

And that means that it is a collection of values, with two very important characteristics:

• An unordered set of values;
• Elements are unique;

See this example:

```a = set([1, 2, 3, 4, 3]);
print(a)
```

This returns the following value:

`{1, 2, 3, 4}`

That is, the second value 3 is ignored as the set needs to have only unique values

Also, we have methods that are used in sets that return a set

We can mention the union and also the intersection

The union method will execute the concatenation of two sets, as it returns a set, only unique values are returned

See an example:

```a = {1,4,5,6}

b = {2,3,12,4,5,7,3}

print(a.union(b))
```

The result will be:

`{1, 2, 3, 4, 5, 6, 7, 12}`

The intersection, on the other hand, will return only the elements that are present in both sets.

See an example:

```a = {1,4,5,6}

b = {2,3,12,4,5,7,3}

print(a.intersection(b))
```

The output will be:

`{4, 5}`

So this is how we use the set, when we need to have sets of unique values from the data that we have in our system

Also pay attention to the Python code style guide, as many call it: Pythonic code

See how to get started in this article, where I mention the main advantages and what this manifesto preaches

Conclusion

In this article we learned what the set is for in Python

A collection whose main characteristic is to have only unique values and, in addition, to not be ordered

We have seen methods that can be used in the set, such as union and intersection

Subscribe
Notify of 