• 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.  Sort Files by Date in Python
  • 2.  Check if Number is Between Two Numbers Using Python
  • 3.  What Curly Brackets Used for in Python
  • 4.  How to Find the Longest String in List in Python
  • 5.  Get Month Name from Datetime in pandas DataFrame
  • 6.  Does Python Use Semicolons? Why Python Doesn’t Use Semicolons
  • 7.  Remove None From List Using Python
  • 8.  Examples of Recursion in Python
  • 9.  Using Python to Replace Multiple Spaces with One Space in String
  • 10.  Get First Key and Value in Dictionary with Python

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

x