Skip to content
geeksforgeeks
  • 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
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • 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:
Python Set clear() Method
Next article icon

Set add() Method in Python

Last Updated : 07 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

The set.add() method in Python adds a new element to a set while ensuring uniqueness. It prevents duplicates automatically and only allows immutable types like numbers, strings, or tuples. If the element already exists, the set remains unchanged, while mutable types like lists or dictionaries cannot be added due to their unhashable nature. Example:

Python
a = set() a.add('s') print(a)  # adding 'e' again a.add('e') print(a)  # adding 's' again a.add('s') print(a) 

Output
{'s'} {'s', 'e'} {'s', 'e'} 

Explanation: This code initializes an empty set a, then adds ‘s’ and ‘e’ using the add() method. Since sets store only unique values, adding ‘s’ again has no effect, ensuring each element appears only once.

Set add() Syntax

set.add( elem )

Parameter: elem is the element to be added to the set.

Returns: It does not return anything (None).

Set add() Method Examples

Example 1: In this example, we have a set of characters and we use the add() method to insert a new element. Since sets only store unique values, adding the same element multiple times has no effect.

Python
a = {'g', 'e', 'k'}  # adding 's' a.add('s') print(a)  # adding 's' again a.add('s') print(a) 

Output
{'g', 's', 'e', 'k'} {'g', 's', 'e', 'k'} 

Explanation: This code initializes a set a with elements ‘g’, ‘e’, and ‘k’, then adds ‘s’ using the add() method. Since sets store only unique values, adding ‘s’ again has no effect, ensuring each element appears only once.

Example 2: In this example, we have a set of numbers and we use the add() method to insert a new element. Since sets only store unique values, adding the same element multiple times has no effect.

Python
a = {6, 0, 4}  # adding 1 a.add(1) print(a)  # adding 0 a.add(0) print(a) 

Output
{0, 1, 4, 6} {0, 1, 4, 6} 

Explanation: This code initializes a set a with elements 6, 0 and 4, then adds 1 using the add() method. Since sets store only unique values, adding 0 again has no effect, ensuring each element appears only once.

Example 3: In this example, we have a set of characters and use the add() method to insert a tuple, while the update() method is used to add elements from a list. Since sets only store unique values, duplicates are ignored.

Python
s = {'g', 'e', 'e', 'k', 's'} t = ('f', 'o') l = ['a', 'e']  # adding tuple t to set s. s.add(t)  # adding list l to set s. s.update(l) print(s) 

Output
{'a', 'g', 'e', 'k', 's', ('f', 'o')} 

Explanation: The code initializes a set s, automatically removing duplicate ‘e’. It adds the tuple t ((‘f’, ‘o’)) using add(), as tuples are hashable, and inserts elements from list l ([‘a’, ‘e’]) using update(). Since sets store unique values, duplicate ‘e’ is ignored.

Read More on Set Methods



Next Article
Python Set clear() Method
author
pawan_asipu
Improve
Article Tags :
  • Python
  • python-set
Practice Tags :
  • python
  • python-set

Similar Reads

  • Python Set Methods
    A Set in Python is a collection of unique elements which are unordered and mutable. Python provides various functions to work with Set. In this article, we will see a list of all the functions provided by Python to deal with Sets. Adding and Removing elementsWe can add and remove elements form the s
    2 min read
  • Set add() Method in Python
    The set.add() method in Python adds a new element to a set while ensuring uniqueness. It prevents duplicates automatically and only allows immutable types like numbers, strings, or tuples. If the element already exists, the set remains unchanged, while mutable types like lists or dictionaries cannot
    5 min read
  • Python Set clear() Method
    Python Set clear() method removes all elements from the set. Python Set clear() Method Syntax: Syntax: set.clear() parameters: The clear() method doesn't take any parameters. Return: None Time complexity : The time complexity of set.clear() function on a set with n element is O(n) . Example 1: Pytho
    2 min read
  • set copy() in python
    The copy() method returns a shallow copy of the set in python. If we use "=" to copy a set to another set, when we modify in the copied set, the changes are also reflected in the original set. So we have to create a shallow copy of the set such that when we modify something in the copied set, change
    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.dis
    3 min read
  • Python Set | difference_update()
    The difference_update() method helps in an in-place way of differentiating the set. The previously discussed set difference() helps to find out the difference between two sets and returns a new set with the difference value, but the difference_update() updates the existing caller set.If A and B are
    1 min read
  • Python Set difference()
    In Python, the difference() method is used to find elements that exist in one set but not in another. It returns a new set containing elements from the first set that are not present in the second set. This operation is similar to the subtraction of sets (A - B), where only unique elements from the
    2 min read
  • issuperset() in Python
    Python Set issuperset() method returns True if all elements of a set B are in set A. Then Set A is the superset of set B. Python issuperset() Method Syntax: Syntax: A.issuperset(B) Parameter: Any other Set to compare with Return: boolean value Python issuperset() exampleExample 1: Working of issubse
    1 min read
  • Python Set issubset() Method
    Python set issubset() method is used with sets to check whether all elements of one set are present in another set. It returns True if every element of the set is found in the other set otherwise, it returns False. Example: [GFGTABS] Python a = {1, 2, 3, 4, 5, 6} b = {4, 5, 6} res = a.issubset(b) pr
    3 min read
  • Python Set isdisjoint() Method
    Python set isdisjoint() function check whether the two sets are disjoint or not, if it is disjoint then it returns True otherwise it will return False. Two sets are said to be disjoint when their intersection is null. Python set isdisjoint() Method Syntax: Syntax: set1.isdisjoint(set2) Parameters: a
    2 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