Python set() Function: Usage Guide

The set() function is used to create a set, which is an unordered collection of unique data. It takes an iterable object (such as a list, tuple, string, etc.) as a parameter, automatically removing any duplicate elements and returning a set containing only unique elements.

For example:

my_list = [1, 2, 3, 1, 2]
my_set = set(my_list)
print(my_set)  # 输出:{1, 2, 3}

Additionally, you can create a set using curly braces {}, but using the set() function is more universal and intuitive.

bannerAds