set() Constructor in Python Last Updated : 10 Nov, 2024 Comments Improve Suggest changes Like Article Like Report In Python, the set() constructor is used to create a set object. A set is a built-in data type that stores an unordered collection of unique elements. The set() constructor can be used to create an empty set or convert other data types (like lists, tuples, or strings) into a set.Example: Python # Example of set constructor a = set() # creates an empty set b = set([1, 2, 2, 3]) # converts a list into a set c = set("hello") # converts a string into a set print(a, type(a)) print(b, type(b)) print(c, type(c)) Outputset() <class 'set'> {1, 2, 3} <class 'set'> {'o', 'l', 'h', 'e'} <class 'set'> Syntax of set()set([iterable])iterable: This is an optional argument. If provided, it should be an iterable object (like a list, tuple, or string). The set() function will convert this iterable into a set.If no iterable is passed, set() will create an empty set.Use of set() ConstructorThe set() constructor is commonly used for:Creating an empty set.Converting other data types (like lists, tuples, or strings) into sets.Storing unique elements (duplicates are automatically removed).Let’s explore a few examples to understand how this works in practice.Convert a List to a SetIf you have a list with some duplicate values, you can convert it into a set to remove duplicates: Python my_list = [1, 2, 2, 3, 4, 4, 5] unique_set = set(my_list) print(unique_set) Output{1, 2, 3, 4, 5} Convert a String to a SetWhen you convert a string to a set, each character in the string becomes an individual element of the set: Python my_string = "banana" unique_chars = set(my_string) print(unique_chars) Output{'n', 'a', 'b'} Comment More infoAdvertise with us Next Article set() Constructor in Python P pragya22r4 Follow Improve Article Tags : Python Practice Tags : python Similar Reads Constructors in Python In Python, a constructor is a special method that is called automatically when an object is created from a class. Its main role is to initialize the object by setting up its attributes or state. The method __new__ is the constructor that creates a new instance of the class while __init__ is the init 3 min read list() constructor in Python In Python list() constructor is a built-in function which construct a list object. We can use list constructor to create an empty list or convert an iterable (dictionary, tuple, string etc.) to a list. Python# Example of list constructor a = list() # creates an empty list b = list((1, 2, 3)) # cover 2 min read dict() Constructor in Python In Python, the dict() constructor is used to create a dictionary object. A dictionary is a built-in data type that stores data in key-value pairs, similar to a real-life dictionary where each word (the key) has a meaning (the value). The dict() constructor helps you create empty dictionaries or conv 2 min read set() Function in python set() function in Python is used to create a set, which is an unordered collection of unique elements. Sets are mutable, meaning elements can be added or removed after creation. However, all elements inside a set must be immutable, such as numbers, strings or tuples. The set() function can take an i 3 min read Calling a Super Class Constructor in Python Classes are like creating a blueprint for an object. If we want to build a building then we must have the blueprint for that, like how many rooms will be there, its dimensions and many more, so here the actual building is an object and blueprint of the building is a class. A Class is a user-defined 4 min read Getter and Setter in Python In Python, a getter and setter are methods used to access and update the attributes of a class. These methods provide a way to define controlled access to the attributes of an object, thereby ensuring the integrity of the data. By default, attributes in Python can be accessed directly. However, this 4 min read Collections.UserDict in Python An unordered collection of data values that are used to store data values like a map is known as Dictionary in Python. Unlike other Data Types that hold only a single value as an element, Dictionary holds key:value pair. Key-value is provided in the dictionary to make it more optimized. Note: For mo 2 min read Convert String to Set in Python There are multiple ways of converting a String to a Set in python, here are some of the methods.Using set()The easiest way of converting a string to a set is by using the set() function.Example 1 : Pythons = "Geeks" print(type(s)) print(s) # Convert String to Set set_s = set(s) print(type(set_s)) pr 1 min read Python Introduction Python was created by Guido van Rossum in 1991 and further developed by the Python Software Foundation. It was designed with focus on code readability and its syntax allows us to express concepts in fewer lines of code.Key Features of PythonPythonâs simple and readable syntax makes it beginner-frien 3 min read Sets in Python A Set in Python is used to store a collection of items with the following properties.No duplicate elements. If try to insert the same item again, it overwrites previous one.An unordered collection. When we access all items, they are accessed without any specific order and we cannot access items usin 9 min read Like