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 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.
What is ChatGPT?
Discover the power of ChatGPT - the cutting-edge language model trained by OpenAI. Learn how ChatGPT is changing the game in NLP.
What is a localhost (127.0.0.1)?
Learn about the benefits of using localhost for web development. Discover how to use it effectively in this comprehensive guide.
What is Business Intelligence?
Unlock insights and drive growth with Business Intelligence. Learn the benefits and best practices for effective data analysis.
What is OneDrive?
Access your files from anywhere with OneDrive. Securely store and share your photos, videos, and documents in the cloud. Get started today!
What does a DevOps Engineer do?
Maximize Efficiency: Learn About the Job Role of a DevOps Engineer and How They Streamline the Software Delivery Process.
What is Continuous Integration?
Optimize software development process with continuous integration. Automate builds, tests & deployments for efficient software delivery.
What is an Algorithm?
Discover the world of algorithms and their practical applications. Learn how algorithms impact daily life. Get started now.
What is DevOps?
Unlock the potential of DevOps to optimize software development and deployment. Improve collaboration, efficiency, and innovation. Learn more!
What does On-Premises mean?
Maximize control and security with on-premise solutions. Discover the benefits of hosting software and data locally. Explore on-premise options!
Other Articles on the Topic of Python Sets
- w3schools offers detailed examples of Python sets with the possibility to execute code snippets directly online.