Python sets are used to store multiple elements in a single variable. The set is mainly used when multiple repetitions of an element are not allowed. The elements within a set are therefore unique.
A Python set is defined by writing the individual values separated by commas in curly brackets. The elements can also have different data types and still be stored in the same set.
As a second way to define a Python set, you can also use the “set” operator and write the elements in double-round brackets. However, this approach is used relatively rarely.
How to query elements?
Since the set is not ordered and the order is therefore irrelevant, individual elements cannot be queried via an index or a key as we know it from the Python list or the dictionary. The only possibility that remains is to query whether a particular element occurs in the set.
How to add elements?
An existing set can be extended by individual elements or even a whole set can be added to an existing one.
It is noticeable that the value “new element” occurs only once in the final set. So if we want to add values to a Python set that already exist in the set, they are simply ignored.
Due to the fact that the four data types are closely connected in Python, we can also connect variables with different data types. For example, lists can also be added to an existing set. Duplicate elements in the list are of course only considered once in the set.
How to delete elements?
If we want to delete elements from a Python set, there are also different possibilities. With the commands “remove” or “discard” we can delete the element by specifying a certain value. With “pop”, on the other hand, we delete the element that was added to the set last. Finally, there is “clear”. This command deletes all elements of the set and leaves a variable with an empty set.
How can multiple Sets be merged?
When merging multiple Python sets, only the elements that appear at least once in both sets are kept. This happens with the so-called “union” function:
The “union” function automatically creates a new object that is stored in addition to the previous two. If this is not desired, you can also explicitly add the elements to an existing Python set. To do this, use the “update” function:
What Python collections are available?
In Python, there are a total of four data types that are stored by default:
- The list is an ordered collection of elements, which is changeable and can also contain duplicate elements.
- The tuple is in effect a list, with the difference that it is no longer changeable. So no elements can be added or removed afterward.
- The set does not allow duplicate entries. At the same time, the arrangement of the elements within the set is variable. The set itself can be changed, but the individual elements cannot be changed afterward.
- Since Python version 3.7, a dictionary is an ordered collection of key-value pairs that can be changed. In the earlier versions, the dictionary is unordered.
This is what you should take with you
- Python Set is one of four pre-installed data structures in Python.
- It is used to store several unique elements in a single variable. The order of the elements does not matter for the time being.
- The Python set is comparable to the mathematical set and accordingly, the same functions can be executed with it, such as the union.
Thanks to Deepnote for sponsoring this article! Deepnote offers me the possibility to embed Python code easily and quickly on this website and also to host the related notebooks in the cloud.
What is Quantum Computing?
Dive into the quantum revolution with our article of quantum computing. Uncover the future of computation and its transformative potential.
What is Anomaly Detection?
Discover effective anomaly detection techniques in data analysis. Detect outliers and unusual patterns for improved insights. Learn more now!
What is the T5-Model?
Unlocking Text Generation: Discover the Power of T5 Model for Advanced NLP Tasks - Learn Implementation and Benefits.
What is MLOps?
Discover the world of MLOps and learn how it revolutionizes machine learning deployments. Explore key concepts and best practices.
What is Jupyter Notebook?
Learn how to boost your productivity with Jupyter notebook! Discover tips, tricks, and best practices for data science and coding. Get started now.
Other Articles on the Topic of Python Sets
- w3schools offers detailed examples of Python sets with the possibility to execute code snippets directly online.
Niklas Lang
I have been working as a machine learning engineer and software developer since 2020 and am passionate about the world of data, algorithms and software development. In addition to my work in the field, I teach at several German universities, including the IU International University of Applied Sciences and the Baden-Württemberg Cooperative State University, in the fields of data science, mathematics and business analytics.
My goal is to present complex topics such as statistics and machine learning in a way that makes them not only understandable, but also exciting and tangible. I combine practical experience from industry with sound theoretical foundations to prepare my students in the best possible way for the challenges of the data world.