Open In App

Python – Append Multiple elements in set

Last Updated : 05 Jul, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In Python, sets are an unordered and mutable collection of data type what does not contains any duplicate elements. In this article, we will learn how to append multiple elements in the set at once.

Example:

Input: test_set = {6, 4, 2, 7, 9}, up_ele = [1, 5, 10]
Output: {1, 2, 4, 5, 6, 7, 9, 10}
Explanation: All elements are updated and reordered. (5 at 3rd position).

Input: test_set = {6, 4, 2, 7, 9}, up_ele = [1, 5, 8]
Output: {1, 2, 4, 5, 6, 7, 8, 9, 10}
Explanation: All elements are updated and reordered. (8 at 7th position).

Append Multiple Elements in Set in Python

There are various ways by which we can append elements given in a list to a Set in Python. They are as follows:

Using update() Method

In this method, we will use Python‘s in-built set update() function to get all the elements in the list aligned with the existing set.

Python3




# initializing set
test_set = {6, 4, 2, 7, 9}
 
# printing original set
print("The original set is : " + str(test_set))
 
# initializing adding elements
up_ele = [1, 5, 10]
 
# update() appends element in set
# internally reorders
test_set.update(up_ele)
 
# printing result
print("Set after adding elements : " + str(test_set))


Output:

The original set is : {2, 4, 6, 7, 9}
Set after adding elements : {1, 2, 4, 5, 6, 7, 9, 10}

Using | Operator (Pipe Operator)

The pipe operator internally calls the union() function, which can be used to perform the task of updating the Python set with new elements. 

Python3




# initializing set
test_set = {6, 4, 2, 7, 9}
 
# printing original set
print("The original set is : " + str(test_set))
 
# initializing adding elements
up_ele = [1, 5, 10]
 
# | performing task of updating
test_set |= set(up_ele)
 
# printing result
print("Set after adding elements : " + str(test_set))


Output:

The original set is : {2, 4, 6, 7, 9}
Set after adding elements : {1, 2, 4, 5, 6, 7, 9, 10}

Using List Comprehension

Here, we will use the Python list comprehension method to append only those elements in a set that are not already present in it. Then we use the set() constructor to convert the list to a Python set.

Python3




# initializing set
test_set = {6, 4, 2, 7, 9}
test_list = list(test_set)
 
# printing original list
print("The original set is : " + str(test_list))
 
# initializing adding elements
up_ele = [1, 5, 10]
 
# adding elements to list using list comprehension
test_list += [ele for ele in up_ele if ele not in test_list]
 
# printing result
print("Set after adding elements : " + str(set(test_list)))
#This code is contributed by Vinay Pinjala.


Output:

The original set is : [2, 4, 6, 7, 9]
Set after adding elements : {1, 2, 4, 5, 6, 7, 9, 10}

Using reduce() Method

This approach uses the reduce() function from the functools module to apply a union operation between each element of a list and the set, resulting in a new set in Python. The reduce() function takes a lambda function and the union() function

Python3




# import functools
from functools import reduce
 
# initializing set
test_set = {6, 4, 2, 7, 9}
 
# printing original list
print("The original list is : " + str(test_set))
 
# initializing adding elements
up_ele = [1, 5, 10]
 
# using reduce and union function to append elements to set
result_set = reduce(lambda res, ele: res.union(set([ele])), up_ele, test_set)
 
# printing result
print("Set after adding elements : " + str(result_set))


Output:

The original list is : {2, 4, 6, 7, 9}
Set after adding elements : {1, 2, 4, 5, 6, 7, 9, 10}


Previous Article
Next Article

Similar Reads

Python | Append multiple lists at once
There can be an application requirement to append elements of 2-3 lists to one list in Python. This kind of application has the potential to come into the domain of Machine Learning or sometimes in web development as well. In this article, we will learn about Python Append Multiple Items to List at Once. Example: Input: list1 = [1, 3, 5, 5, 4] list
8 min read
Python - Append Missing elements from other List
Given 2 lists, append elements missing from list 1 to list 2. Input : test_list1 = [5, 6, 4, 8, 9, 1], test_list2 = [9, 8, 10] Output : [5, 6, 4, 1, 9, 8, 10] Explanation : 5, 6, 4, 1 added to list 2, in order. Input : test_list1 = [5, 6, 4, 8, 9, 1], test_list2 = [9, 10] Output : [5, 6, 4, 8, 1, 9, 10] Explanation : 5, 6, 4, 8, 1 added to list 2,
7 min read
How to append elements in Python tuple?
In this article, we will explore how to append elements to a tuple in Python. Appending elements to a tuple in Python may additionally seem counterintuitive due to the immutability of tuples. Understanding TuplesBefore we dive into appending elements to tuples, permit's quick evaluation of what tuples are and why they're beneficial. Tuples are orde
2 min read
Append Tuple To A Set With Tuples
Tuples and sets in Python have some key differences. Tuples are ordered collections of elements that can't be changed once created, denoted by parentheses, and they allow duplicates. You can access specific elements using indexing and slicing. On the other hand, sets are unordered collections of unique elements, represented by curly braces. They ca
4 min read
append() and extend() in Python
Extend and Append are two Python list methods used to add elements to a list. Although they appear similar, they have different functionalities and use cases. Understanding the differences between the append() and extend() methods is crucial when working with lists in Python. Although both techniques are used to add elements to a list, their behavi
4 min read
Python | Pandas Index.append()
Python is an excellent language for data analysis, primarily because of the fantastic ecosystem of data-centric Python packages. Pandas are one of those packages, making importing and analyzing data much easier. Pandas Index.append() The function is used to append a single or a collection of indices together. In the case of a collection of indices,
2 min read
Python | Pandas TimedeltaIndex.append()
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas TimedeltaIndex.append() function appends a collection of index options together. More than one index object can be appended at a
2 min read
Python | Pandas Series.append()
Pandas series is a One-dimensional ndarray with axis labels. The labels need not be unique but must be a hashable type. The object supports both integer- and label-based indexing and provides a host of methods for performing operations involving the index. Pandas Series.append() function is used to concatenate two or more series object. Syntax: Ser
4 min read
Python | Append at front and remove from rear
Being familiar with the concept of queue, which follows the FIFO rule, i.e first in first out, that suggests a front removal and rear insertion. This has been discussed many times. But sometimes we need to perform the exact opposite of this and we need to perform the append at the front and remove the element from the rear. Let's discuss certain wa
8 min read
Python | Append Odd element twice
Given a list of numbers, the task is to create a new list from the initial list with the condition to append every odd element twice. Below are some ways to achieve the above task. Method #1: Using list comprehension C/C++ Code # Python code to create a new list from initial list # with condition to append every odd element twice. # List initializa
4 min read
Practice Tags :