Skip to content

Python Sets – easily explained!

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.

Python 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.

Python 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.

Python Set

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.

Python 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.

Python 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:

Python Set

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:

Python Set

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.

Other Articles on the Topic of Python Sets

  • w3schools offers detailed examples of Python sets with the possibility to execute code snippets directly online.
close
Das Logo zeigt einen weißen Hintergrund den Namen "Data Basecamp" mit blauer Schrift. Im rechten unteren Eck wird eine Bergsilhouette in Blau gezeigt.

Don't miss new articles!

We do not send spam! Read everything in our Privacy Policy.

Tags:
Cookie Consent with Real Cookie Banner