sec02 - Python set: Overview and Basics
スポンサーリンク

What is a set – Basics and Characteristics of Python Sets

A set is a collection of elements that has no duplicates and does not preserve order.

The main use cases of sets are as follows:

  • Membership test (checking whether a value exists in a set)
  • Removing duplicates from lists and other collections
  • Performing mathematical operations such as intersection, union, difference, and symmetric difference (exclusive OR)

Since sets are unordered, they do not record insertion order or element position. Therefore, sets do not support operations using indexes or other sequence-like behavior.

Initializing a set – Creating empty and non-empty sets

First, let’s initialize a set. Write code as follows:

# set with 0 elements
s1 = set()
print(s1, type(s1))  # set() <class 'set'>

# set with elements
s2 = {'0', '1', '2'}
print(s2, type(s2))  # {'2', '0', '1'} <class 'set'> (※ Since order is not guaranteed, the output order may vary)

When a set has no elements, initialize it with the set() function. When it has elements, use curly braces {} and separate each element with a comma. In Python, if curly braces simply contain elements separated by commas, it is recognized as a set. If elements are defined in key:value form, it is recognized as a dict. If you write var = {}, Python treats it as a dict. Therefore, to define an empty set, you must use the set() function. (set() is also used for type conversion.)

A set does not necessarily need to contain elements. You can initialize it as an empty set and later add elements using methods such as add(). (The method of adding elements will be explained in a dedicated lecture.)

A set does not maintain the order of elements. For example, if you define {'0', '1', '2'} in line 6 of the above code and print it, the result may look like {'2', '0', '1'}. (※ Since order is not guaranteed, the output order may vary.)

スポンサーリンク

Data types allowed and not allowed as set elements

The following Python standard data types cannot be added as set elements. These data structures are characterized by being mutable (modifiable). (You cannot add a set inside another set.)

  • list
  • dict
  • set

The following Python standard data types can be added as set elements. These types are immutable (cannot be modified).

  • int/ float
  • str
  • tuple
    • However, the elements inside the tuple must also be of immutable data types. If the tuple contains mutable types such as lists or dicts, an error will occur.

Nested sets

As explained earlier, a set cannot be nested inside another set. This restriction applies not only to direct nesting but also when a set is contained within a tuple.

s1 = {1, (2, (3, 4))}  # ✅ OK
s2 = {1, (2, {3, 4})}  # ❌ Error

In the above code, a tuple containing another tuple ((2, (3, 4))) is valid, but a tuple containing a set ((2, {3, 4})) is not allowed.

Sets with multiple data types – mixing int/float, str, and tuple

In practice, it is easier to manage sets when they contain only one type of data, such as a “set of numbers” or a “set of strings.” However, as shown in the following code, a single set can contain different data types.

mixed = {1, 'two', 3.0, (4, 4.0)}
print(mixed, type(mixed))  # {(4, 4.0), 1, 'two', 3.0} <class 'set'>

The set above contains the data types int, string, float, and tuple. This shows that Python sets can easily manage multiple kinds of data.

As explained earlier, list, dict, and set cannot be included as elements.

Retrieving elements from a set – using pop() to get random items

Since sets do not maintain order like lists or strings, you cannot retrieve elements using an expression like [n]. You can use the pop() method to retrieve one element, but note that the element is chosen completely at random.

mixed = {1, 'two', 3.0, (4, 4.0)}
print(mixed.pop())  # (4, 4.0)
print(mixed.pop())  # 1
print(mixed.pop())  # 3.0
print(mixed.pop())  # two

Number of set elements – checking with the len() function

To check the number of elements in a set, use the len() function.

mixed = {1, 'two', 3.0, (4, 4.0)}
print(len(mixed))  # 4
スポンサーリンク