Skip to content
  • (+91) 9409548155
  • support@appdividend.com
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
Menu
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
Python

Adding Multiple Elements to a Set in Python

  • 17 Feb, 2025
  • Com 0
Adding multiple elements to a Set

Sets in Python are unordered collections that only contain unique elements. The set itself is not hashable, but it must contain hashable elements.

When adding multiple elements, you cannot use a set.add() method directly because it will add one element at a time. However, you can use it with a for loop, but that’s not an efficient approach.

Here are three main ways:

  1. Using set.update()
  2. Using | operator
  3. Using set.union()

Method 1: Using set.update()

Using set.update() to append multiple elements to a Set in Python

The set.update() method is the most efficient and Pythonic way to add elements to a set, accepting any iterable and adding its elements without throwing errors.

main_set = {11, 21, 31}

set_to_add = {41, 51, 61}

main_set.update(set_to_add)

print(main_set)

# {51, 21, 41, 11, 61, 31}

The above output shows a set with appended elements, and the order has been changed.

Duplicate elements

Removing duplicates while appending

If some of the iterable elements are already in the set, the .update() method will ignore them because duplicate elements are not allowed in the set.

main_set = {11, 21, 31}

set_to_add = {41, 21, 31}  # 21 and 31 are already in main_set

main_set.update(set_to_add)

print(main_set)

# {21, 41, 11, 31}

Adding a list to a set

We can take a list as an iterable, make it a source of multiple individual elements, and append it to a set.

main_set = {11, 21, 31}

list = [41, 51, 61]  # list to be added to the set

main_set.update(list)

print(main_set)

# {41, 11, 51, 21, 61, 31}

And it works as expected! The above output shows that .update() method did not add a list as a single element.

We cannot add a list itself as an element to a set because lists are not hashable.

Adding a tuple to a set

A tuple is also iterable, and you can take it as a source of multiple individual elements.

main_set = {11, 21, 31}

tuple = (41, 51, 61)  # tuple to be added to the set

main_set.update(tuple)

print(main_set)

# {41, 11, 51, 21, 61, 31}

Method 2: Using | (union) operator

Using | Operator to get a new combined set

The “|” operator unions two sets and returns a new set containing all unique elements from both sets.

main_set = {11, 21, 31}

set_to_append = {41, 51, 61}  # Another set

new_set = main_set | set_to_append

print(new_set)

# {41, 11, 51, 21, 61, 31}

Here, we get the new set in the output with combined elements.

Appending multiple elements using set union operator (_)

To modify the original set, you can use the |= operator.

main_set = {11, 21, 31}

set_to_append = {41, 51, 61}  # Another set

main_set |= set_to_append

print(main_set)

# {41, 11, 51, 21, 61, 31}

The union operator (|) only works for sets. This means you can only combine two sets at a time. You cannot union a set with a tuple or list directly.

If you try, you’ll get a TypeError like this: TypeError: unsupported operand type(s) for |=: ‘set’ and ‘tuple’

main_set = {11, 21, 31}

tuple = (41, 51, 61)

main_set |= tuple

print(main_set)

# TypeError: unsupported operand type(s) for |=: 'set' and 'tuple'

Method 3: Using union()

Appending multiple elements using set.union() method

The Set union() method creates a new set with combined elements rather than modifying the original set in place. It’s a good choice for the new set, which contains all the elements.

main_set = {11, 21, 31}

set_to_append = {41, 51, 61}  # Another set

new_set = main_set.union(set_to_append)  # Union of two sets

print(new_set)

# {51, 21, 41, 11, 61, 31}

Using a for loop with the set.add()

You can add numerous elements one by one using the for loop and the set.add() function. However, it does not append all elements at once, making it less efficient due to multiple function calls.

main_set = {11, 21, 31}

elements_to_add = [4, 5, 6]  # List

for element in elements_to_add:
    main_set.add(element)

print(main_set)

# {4, 5, 6, 11, 21, 31}

You can also use list comprehension and the functools.reduce() method, but there is no need to cover all these ways because what we have covered is enough.

Post Views: 24
Share on:
Krunal Lathiya

With a career spanning over eight years in the field of Computer Science, Krunal’s expertise is rooted in a solid foundation of hands-on experience, complemented by a continuous pursuit of knowledge.

How to Set Cell Values in Pandas DataFrame
How to Convert Set to List in Python

Leave a Reply Cancel reply

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

Address: TwinStar, South Block – 1202, 150 Ft Ring Road, Nr. Nana Mauva Circle, Rajkot(360005), Gujarat, India

Call: (+91) 9409548155

Email: support@appdividend.com

Online Platform

  • Pricing
  • Instructors
  • FAQ
  • Refund Policy
  • Support

Links

  • About Us
  • Contact Us
  • Privacy Policy
  • Terms of services

Tutorials

  • Angular
  • React
  • Python
  • Laravel
  • Javascript
Copyright @2024 AppDividend. All Rights Reserved
Appdividend