The world’s leading publication for data science, AI, and ML professionals.

5 Basic Commands When Working with Python Sets

Making you understand the characteristics of Python Lists and how you deal with them

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.

1. Defining a Python Set

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.

2. Query Elements from a Python Set

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.

3. Adding 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.

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

5. Merge Multiple Sets

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:

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.

_If you like my work, please subscribe here or check out my website Data Basecamp! Also, medium permits you to read 3 articles per month for free. If you wish to have unlimited access to my articles and thousands of great articles, don’t hesitate to get a membership for $5 per month by clicking my referral link:_ https://medium.com/@niklas_lang/membership


6 Pandas DataFrame Tasks Anyone Learning Python Should Know

4 Basic Commands When Working with Python Tuples

An Introduction to TensorFlow


Related Articles