set() method is used to convert any of the iterable to a sequence of iterable elements with distinct elements, commonly called Set. In Python, the set() function is a built-in constructor that is used to initialize a set or create an empty. In this article, we will see about set() in Python and how we can convert an iterable to a sequence with unique elements in Python.
Python set() Method Syntax
Syntax: set(iterable)
Parameters : Any iterable sequence like list, tuple or dictionary.
Returns : An empty set if no element is passed. Non-repeating element iterable modified as passed as argument.
What is Python set() Function?
Set, a term in mathematics for a sequence consisting of distinct languages is also extended in its language by Python and can easily be made using set(). set() method is used to convert an iterable to a sequence with unique elements in Python, commonly called Set. It is a built-in constructor function that is used to create an empty set or initialize a set with elements.
Properties of Python set() Method
- No parameters are passed to create the empty set
- The dictionary can also be created using a set, but only keys remain after conversion, and values are lost.
set() Function in Python Examples
Below are the ways by which we can use set() in Python:
- Creating an Empty Set
- Using set() with List
- Using set() with Tuples
- Creating set with Range
- Converting Dictionary into a Set
Creating a Set by using set() Function
In this example, we are creating a Set using set() function.
Python3
s = set ()
print ( "Type of s is " , type (s))
|
Output
Type of s is <class 'set'>
set() Function with List
In this example, we are using set() with List. Here, we will convert an iterable to a sequence with unique elements in Python.
Python3
lis1 = [ 3 , 4 , 1 , 4 , 5 ]
print ( "The list before conversion is : " + str (lis1))
print ( "The list after conversion is : " + str ( set (lis1)))
|
Output
The list before conversion is : [3, 4, 1, 4, 5]
The list after conversion is : {1, 3, 4, 5}
set() Function with Tuple
In this example, we are using set() function with tuple.
Python3
tup1 = ( 3 , 4 , 1 , 4 , 5 )
print ( "The tuple before conversion is : " + str (tup1))
print ( "The tuple after conversion is : " + str ( set (tup1)))
|
Output
The tuple before conversion is : (3, 4, 1, 4, 5)
The tuple after conversion is : {1, 3, 4, 5}
set() Function with Range
In this example, we are using set() function with range function. Here, we will convert an iterable to a sequence with unique elements in Python.
Python3
r = range ( 5 )
r = set (r)
print ( "The Range after conversion is : " + str (r))
|
Output
The Range after conversion is : {0, 1, 2, 3, 4}
Demonstration of set() Method with Dictionary
In this example, we are seeing the demonstration of set() with Dictionary and it’s working.
Python3
dic1 = { 4 : 'geeks' , 1 : 'for' , 3 : 'geeks' }
print ( "Dictionary before conversion is : " + str (dic1))
print ( "Dictionary after conversion is : " + str ( set (dic1)))
|
Output
Dictionary before conversion is : {4: 'geeks', 1: 'for', 3: 'geeks'}
Dictionary after conversion is : {1, 3, 4}
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
29 Nov, 2023
Like Article
Save Article