DevAcademia
C++C#CPythonJava
  • Python Fundamentals

  • Introduction to Python
  • Getting Started with Python
  • Python Syntax
  • Python Comments
  • Python Variables
  • Python Data Types
  • Python Numbers
  • Python Casting
  • Python Strings
  • Python Booleans
  • Python Operators
  • Python Lists
  • Python Tuples
  • Python Sets
  • Python Dictionaries
  • Python If...Else
  • Python Match
  • Python While Loops
  • Python For Loops
  • Python Functions
  • Python Lambda
  • Python Arrays
  • Python OOP

  • Python OOP
  • Python Constructors
  • Python Destructors
  • Python Classes/Objects
  • Python Inheritance
  • Python Polymorphism
  • Python Quiz

  • Python Fundamentals Quiz
  • Python Fundamentals

  • Introduction to Python
  • Getting Started with Python
  • Python Syntax
  • Python Comments
  • Python Variables
  • Python Data Types
  • Python Numbers
  • Python Casting
  • Python Strings
  • Python Booleans
  • Python Operators
  • Python Lists
  • Python Tuples
  • Python Sets
  • Python Dictionaries
  • Python If...Else
  • Python Match
  • Python While Loops
  • Python For Loops
  • Python Functions
  • Python Lambda
  • Python Arrays
  • Python OOP

  • Python OOP
  • Python Constructors
  • Python Destructors
  • Python Classes/Objects
  • Python Inheritance
  • Python Polymorphism
  • Python Quiz

  • Python Fundamentals Quiz

Loading Python tutorial…

Loading content
Python FundamentalsTopic 48 of 77
←PreviousPrevNextNext→

Add Set Items

Adding Items to Sets

Sets are mutable collections, so new elements can be added after creation.

Use the add() method to insert a single element. If the element is already present, the set remains unchanged.

Use the update() method to add multiple elements from an iterable (such as a list, tuple, set, or string).

Example
# Adding items to sets
fruits = {"apple", "banana", "cherry"}

# Add single item
fruits.add("orange")
print(fruits)

# Add multiple items
fruits.update(["mango", "grape"])
print(fruits)
Output
{'cherry', 'banana', 'apple', 'orange'}
{'cherry', 'banana', 'mango', 'apple', 'grape', 'orange'}

Adding Duplicates

Adding an existing element does not change the set, since sets automatically enforce uniqueness.

Example
# Adding duplicate elements
numbers = {1, 2, 3}
numbers.add(2)
print(numbers)
Output
{1, 2, 3}

Using Update with Different Iterables

The update() method accepts one or more iterables. All elements from the iterables are added to the set.

Example
# Update with multiple iterables
colors = {"red", "green"}
colors.update(["blue", "yellow"], {"black", "white"}, ("pink", "purple"))
print(colors)
Output
{'red', 'purple', 'pink', 'yellow', 'green', 'blue', 'black', 'white'}

Adding Characters from a String

Strings are iterables, so using update() with a string adds each character individually to the set.

Example
# Adding characters from a string
letters = {"a", "b"}
letters.update("cat")
print(letters)
Output
{'a', 'b', 't', 'c'}

Best Practices

Use add() when inserting a single element for clarity.

Use update() for merging multiple items at once from iterables.

Be cautious when using update() with strings, since each character is treated as a separate element.

Remember that sets do not allow duplicates, so re-adding an element has no effect.

Test your knowledge: Add Set Items
Quiz Configuration
4 of 8 questions
Sequential
Previous allowed
Review enabled
Early close allowed
Estimated time: 5 min
Python FundamentalsTopic 48 of 77
←PreviousPrevNextNext→