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 Like