Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
set() Function in python
Next article icon

set() Function in python

Last Updated : 26 Feb, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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 iterable (like a list, tuple or dictionary) as input, removing duplicates automatically. Sets support various operations such as union, intersection and difference, making them useful for mathematical computations and data processing.

Example:

Python
a = set([1, 2, 3, 4, 2, 3])   print(a)   

Output
{1, 2, 3, 4} 

Syntax:

set(iterable)

here, iterable can be anything like list, tuple, dictionary or range. If no argument is provided it will create an empty set.

1. Creating an empty set

In Python, an empty set can be created using the set() function, as curly braces {} create an empty dictionary instead. An empty set is useful for storing unique elements dynamically. Since sets are mutable, elements can be added later using add() or update(). The type() function can be used to verify the data type.

Python
a = set() print(a)   print(type(a))   

Output
set() <class 'set'> 

It will return the empty set.

2. set with list

A list can be converted into a set using set(). This removes duplicate elements and stores only unique values. Since sets are unordered, the order of elements in the original list may not be preserved. This method is useful for filtering out duplicate values from a list.

Python
a = [1, 2, 3, 4, 2, 3, 5] b = set(a) print(b)   

Output
{1, 2, 3, 4, 5} 

List is converted into set.

3. set with tuple()

Tuples, like lists, can be converted into sets using the set() function. Since sets only store unique elements, duplicates from the tuple are removed. This is useful when needing a unique collection of immutable values. The resulting set is unordered and does not retain the tuple's order.

Python
tup = (1, 2, 3, 4, 2, 3, 5) a = set(tup) print(a)   

Output
{1, 2, 3, 4, 5} 

tuple is converted into a set.

4. set with range()

A set can be created using the range() function, which generates a sequence of numbers. When passed to set(), it produces a set containing the unique numbers in the specified range. This method is useful for quickly generating sets of consecutive numbers.

Python
a = set(range(0, 11))   print(a)   

Output
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10} 

range function is used to create set.

5. set with dictionary

When a dictionary is converted into a set using set(), only the keys are stored in the set. The values are ignored unless explicitly extracted and converted. This method is useful when working with unique keys from a dictionary.

Python
d = {'a': 1, 'b': 2, 'c': 3} a = set(d) print(a)   

Output
{'b', 'a', 'c'} 

Output order may vary.


Next Article
set() Function in python

M

manjeet_04
Improve
Article Tags :
  • Python
  • python-set
  • Python-Built-in-functions
  • Python-set-functions
Practice Tags :
  • python
  • python-set

Similar Reads

    sum() function in Python
    The sum of numbers in the list is required everywhere. Python provides an inbuilt function sum() which sums up the numbers in the list. Pythonarr = [1, 5, 2] print(sum(arr))Output8 Sum() Function in Python Syntax Syntax : sum(iterable, start) iterable : iterable can be anything list , tuples or dict
    3 min read
    Python print() function
    The python print() function as the name suggests is used to print a python object(s) in Python as standard output. Syntax: print(object(s), sep, end, file, flush) Parameters: Object(s): It can be any python object(s) like string, list, tuple, etc. But before printing all objects get converted into s
    2 min read
    Python len() Function
    The len() function in Python is used to get the number of items in an object. It is most commonly used with strings, lists, tuples, dictionaries and other iterable or container types. It returns an integer value representing the length or the number of elements. Example:Pythons = "GeeksforGeeks" # G
    2 min read
    Union() function in Python
    Union() method in Python is an inbuilt function provided by the set data type. It is used to combine multiple sets into a single set, containing all unique elements from the given sets. It ensures that no duplicate values exist in the final set.Python Set UnionThe symbol for denoting union of sets i
    3 min read
    Python Functions
    Python Functions is a block of statements that does a specific task. The idea is to put some commonly or repeatedly done task together and make a function so that instead of writing the same code again and again for different inputs, we can do the function calls to reuse code contained in it over an
    9 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences