The Wayback Machine - https://web.archive.org/web/20240612063952/https://www.geeksforgeeks.org/python-set-function/
Open In App

Python | set() Function

Last Updated : 18 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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




# we are creating an 
#empty set by using set()
  
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




# working of set() on list
# initializing list 
lis1 = [ 3, 4, 1, 4, 5 ]
  
# Printing iterables before conversion
print("The list before conversion is : " + str(lis1))
  
# Iterables after conversion are 
# notice distinct and elements
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




# working of set() on tuple
# initializing tuple
tup1 = (3, 4, 1, 4, 5)
  
# Printing iterables before conversion
print("The tuple before conversion is : " + str(tup1))
  
# Iterables after conversion are 
# notice distinct and elements
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




# working of set() on range
  
# initializing range 
r = range(5)
  
r=set(r)
# Iterables after conversion are 
# notice distinct and elements
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




# Python3 code to demonstrate the 
# working of set() on dictionary
  
# initializing list 
dic1 = { 4 : 'geeks', 1 : 'for', 3 : 'geeks' } 
  
# Printing dictionary before conversion
# internally sorted
print("Dictionary before conversion is : " + str(dic1))
  
# Dictionary after conversion are 
# notice lost keys
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}




Similar Reads

Python - Call function from another function
Prerequisite: Functions in Python In Python, any written function can be called by another function. Note that this could be the most elegant way of breaking a problem into chunks of small problems. In this article, we will learn how can we call a defined function from another function with the help of multiple examples. What is Calling a Function
4 min read
Wand function() function in Python
function() function is similar to evaluate function. In function() function pixel channels can be manipulated by applies a multi-argument function to pixel channels. Following are the list of FUNCTION_TYPES in Wand: 'undefined''arcsin''arctan''polynomial''sinusoid' Syntax : wand.image.function(function, arguments, channel) Parameters : ParameterInp
1 min read
Returning a function from a function - Python
Functions in Python are first-class objects. First-class objects in a language are handled uniformly throughout. They may be stored in data structures, passed as arguments, or used in control structures. Properties of first-class functions: A function is an instance of the Object type.You can store the function in a variable.You can pass the functi
3 min read
Accessing Python Function Variable Outside the Function
In Python, variables defined within a function have local scope by default. But to Access function variables outside the function usually requires the use of the global keyword, but can we do it without using Global. In this article, we will see how to access a function variable outside the function without using "Global". Accessing Function Variab
3 min read
Zip function in Python to change to a new character set
Given a 26 letter character set, which is equivalent to character set of English alphabet i.e. (abcd….xyz) and act as a relation. We are also given several sentences and we have to translate them with the help of given new character set. Examples: New character set : qwertyuiopasdfghjklzxcvbnm Input : "utta" Output : geek Input : "egrt" Output : co
2 min read
Python map function | Count total set bits in all numbers from 1 to n
Given a positive integer n, count the total number of set bits in binary representation of all numbers from 1 to n. Examples: Input: n = 3 Output: 4 Binary representations are 1, 2 and 3 1, 10 and 11 respectively. Total set bits are 1 + 1 + 2 = 4. Input: n = 6 Output: 9 Input: n = 7 Output: 12 Input: n = 8 Output: 13 We have existing solution for t
2 min read
Matplotlib.axis.Axis.set() function in Python
Matplotlib is a library in Python and it is numerical – mathematical extension for NumPy library. It is an amazing visualization library in Python for 2D plots of arrays and used for working with the broader SciPy stack. Matplotlib.axis.Axis.set() Function The Axis.set() function in axis module of matplotlib library is a property batch setter. Pass
2 min read
Matplotlib.axis.Tick.set() function in Python
Matplotlib is a library in Python and it is numerical – mathematical extension for NumPy library. It is an amazing visualization library in Python for 2D plots of arrays and used for working with the broader SciPy stack. matplotlib.axis.Tick.set() Function The Tick.set() function in axis module of matplotlib library is a property batch setter. Pass
2 min read
Python Set discard() Function
Python discard() is a built-in method to remove elements from the set. The discard() method takes exactly one argument. This method does not return any value. Example: In this example, we are removing the integer 3 from the set with discard() in Python. C/C++ Code my_set = {1, 2, 3, 4, 5} my_set.discard(3) print(my_set) Output {1,2,4,5}Python Set d
3 min read
Python program to convert Set into Tuple and Tuple into Set
Let's see how to convert the set into tuple and tuple into the set. For performing the task we are use some methods like tuple(), set(), type(). tuple(): tuple method is used to convert into a tuple. This method accepts other type values as an argument and returns a tuple type value.set(): set method is to convert other type values to set this meth
7 min read