Python Program to Append Multiple Elements in Set


In Python, a set is an unordered collection of unique elements, represented by {}. It allows efficient membership testing and eliminates duplicate values, making it useful for tasks such as removing duplicates or checking for common elements between sets.

Appending multiple elements to a set in Python is a common operation that involves adding multiple unique elements to an existing set.

In this article, we will learn how to append multiple elements in a set in python.

Example

Assume we have taken an input set and input list. We will now append the input list elements to the input set using the above methods by removing all the duplicates and ordering them in ascending order.

Input

inputSet = {3, 9, 5, 1, 2, 8}
newList = [2, 10, 4, 3]

Output

Resultant set after adding list elements: {1, 2, 3, 4, 5, 8, 9, 10}

In the above input set, the input list elements are added to the input set, and removed all the duplicates by ordering and printing the result in ascending order.

Methods Used

The following are the various methods to accomplish this task:

  • Using the update() function

  • Using Pipe operator( | )

  • Using extend() function

Method 1: Using the update() function

In this method we are going to learn how to use the update() function in python to append multiple elements in a set. update() function(inserts the given items such as dictionary, or an iterable object with key-value pairs into the dictionary)

Syntax

set_name.update(iterable)

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task

  • Create a variable to store the input set.

  • Print the input set.

  • Create another variable to store the input list for adding elements to the input set.

  • Use the update() function to append the set with the input list elements randomly by passing the input list as an argument to it.

  • Print the resultant set after appending input list elements to it

Example

The following program returns a set after appending input list elements to the input set orderly by removing all duplicates using the update() function –

# input set
inputSet = {3, 9, 5, 1, 2, 8}
# printing input set
print("Input Set:", inputSet)
# input list for adding elements to the set
newList = [2, 10, 4, 3]
# updating the set with the input list elements orderly
inputSet.update(newList)
# printing resultant set after adding input list elements
print("Resultant set after adding list elements:", inputSet)

Output

On executing, the above program will generate the following output –

Input Set: {1, 2, 3, 5, 8, 9}
Resultant set after adding list elements: {1, 2, 3, 4, 5, 8, 9, 10}

Method 2: Using Pipe operator( | )

In this method we are going to learn how to use Pipe operator() function in python to append multiple element in set.pipe operator(|): Internally, the pipe operator(|) invokes union(), which can be used to update a set with more new items.

Syntax

result = operand1 | operand2

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task –.

  • Convert the given list to a set using the set() function.

  • Apply the | operator on the input set and add this new above-created list elements set to the input set.

  • Print the resultant set after appending input list elements to it

Example

The following program returns a set after appending input list elements to the input set orderly by removing all duplicates using the pipe operator(|) –

# input set
inputSet = {3, 9, 5, 1, 2, 8}
# printing input set
print("Input Set:", inputSet)
# input list for adding elements to the set
newList = [2, 10, 4, 3]
# Converting the new list to set
# Applying | operator on the input set to add these set elements to a given set
inputSet |= set(newList)
# printing resultant set after adding input list elements
print("Resultant set after adding list elements:", inputSet)

Output

On executing, the above program will generate the following output

Input Set: {1, 2, 3, 5, 8, 9}
Resultant set after adding list elements: {1, 2, 3, 4, 5, 8, 9, 10}

Method 3: Using extend() function

In this method we will demonstrate how to use an extend() function to append multiple element of a given set. extend() function(adds all the elements of an iterable like list, tuple, string etc to the end of the list)

Syntax

list_name.extend(iterable)

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task

  • Convert the given input set elements to a list using the list() function.

  • Pass the list as an argument to the extend() function to extend the input set elements list with the input list elements orderly.

  • Use the set() function(creates a set object. A set list will appear in random order because the items are not ordered. It removes all the duplicates) to convert the list into a set.

  • Print the resultant set after adding input list elements to the input set.

Example

The following program returns a set after appending input list elements to the input set orderly by removing all duplicates using the extend() function –

# input set
inputSet = {3, 9, 5, 1, 2, 8}
# printing input set
print("Input Set:", inputSet)
# converting input set into a list
setElementsList = list(inputSet)
# input list for adding elements to the set
newList = [2, 10, 4, 3]
# extending the set elements list with the input list elements orderly
setElementsList.extend(newList)
# converting the list into a set
# set() function also removes duplicates
resultant_set = set(setElementsList)
# printing the resultant set after adding input list elements
print("Resultant set after adding list elements:", resultant_set)

Output

On executing, the above program will generate the following output

Input Set: {1, 2, 3, 5, 8, 9}
Resultant set after adding list elements: {1, 2, 3, 4, 5, 8, 9, 10}

Conclusion

In this article, we have learned 3 different methods to Append Multiple elements in the set. We learned how to use the update() function to add the current set with a different set. We learned how to change a list into a set and vice versa. Finally, we learnt how to use the extend() function to extend the list by adding another list.

Updated on: 17-Aug-2023

266 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements