Skip to content

Python Sets Basics

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.

# Define a set
set_1 = {'first element', 2, False, 'Fourth Element'}
set_1 = set(('first element', 2, False, 'Fourth Element'))

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.

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.

# Define set
set_1 = {'first element', 2, False, 'Fourth Element'}

# Check if element in set
print('first element' in set_1)
print('second element' in set_1)

Out: 
True
False

Adding Elements

An existing set can be extended by individual elements or even a whole set can be added to an existing one.

# Define set
set_1 = {'first element', 2, False, 'Fourth Element'}
set_2 = {'new element', 34}

# Add one element to a set
set_1.add('new_element')
print(set_1)

# Add a new set to an existing one
set_1.update(set_2)
print(set_1)

Out:
{False, 2, 'Fourth Element', 'first element', 'new element'}
{False, 2, 34, 'Fourth Element', 'first element', 'new element'}

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.

# Define a set and a list
set_1 = {'first element', 2, False, 'Fourth Element'}
list_2 = ['third element', 'third element']

# Add list to set
set_1.update(list_2)
print(set_1)

Out:
{False, 2, 'third element', 'Fourth Element', 'first element'}

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

# Define a set
set_1 = set_1 = {'first element', 2, False, 'Fourth Element'}

# Remove specific items
set_1.remove('first element')
set_1.discard(2)

# Remove the last element of the set
set_1.pop()

# Clear the whole set
set_1.clear()

Python Collections

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

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