• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / Using Python to Add Items to Set

Using Python to Add Items to Set

August 19, 2022 Leave a Comment

In Python, to add to a set, you can use the add() function. add() will add an element to the set if the element is not already in the set.

s = {1, 2, 3}

s.add(4)

print(s)

#Output:
{1, 2, 3, 4}

You can also use the update() function to add multiple elements from a list to a set.

s = {1, 2, 3}

s.update([4,5])
s.update({6,7})

print(s)

#Output:
{1, 2, 3, 4, 5, 6, 7}

When working with collections of data in Python, the ability to easily add items or change the collection is important.

One such case where you may want to modify a collection is if you want to add elements to a set in Python.

To add on item to a set in Python, you can use the add() function. add() will add an element to the set if the element is not already in the set.

Below shows a simple example of how you can use add() to add one item to a set in Python.

s = {1, 2, 3}

s.add(4)

print(s)

#Output:
{1, 2, 3, 4}

Adding Multiple Items to Set in Python with update()

If you want to add multiple items to a set in Python, then you could use the add() function multiple times, or use the update() function.

The update() function takes in an iterable object (such as a list or set) and adds it to the set.

Below shows you how to add multiple elements to a set using update() in Python.

s = {1, 2, 3}

s.update([4,5])
s.update({6,7})

print(s)

#Output:
{1, 2, 3, 4, 5, 6, 7}

Hopefully this article has been useful for you to learn how to add items to a set in Python.

Other Articles You'll Also Like:

  • 1.  Concatenate Multiple Files Together in Python
  • 2.  Writing a Table Faster to Word Document Using python-docx
  • 3.  Calculating Sum of Squares in Python
  • 4.  math.radians() python – How to Convert Degrees to Radians in Python
  • 5.  Truncate Decimal from Number with Python math.trunc() Function
  • 6.  Using Python to Create List of Prime Numbers
  • 7.  Python turtle dot() – Draw Dot on Turtle Screen
  • 8.  Check if String Does Not Contain Substring in Python
  • 9.  Remove Duplicates from Sorted Array in Python
  • 10.  Add Seconds to Datetime Variable Using Python timedelta() Function

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy