Python – Append Multiple elements in set
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 settest_set = {6, 4, 2, 7, 9}# printing original setprint("The original set is : " + str(test_set))# initializing adding elementsup_ele = [1, 5, 10]# update() appends element in set# internally reorderstest_set.update(up_ele)# printing resultprint("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 settest_set = {6, 4, 2, 7, 9}# printing original setprint("The original set is : " + str(test_set))# initializing adding elementsup_ele = [1, 5, 10]# | performing task of updatingtest_set |= set(up_ele)# printing resultprint("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 settest_set = {6, 4, 2, 7, 9}test_list = list(test_set)# printing original listprint("The original set is : " + str(test_list))# initializing adding elementsup_ele = [1, 5, 10]# adding elements to list using list comprehensiontest_list += [ele for ele in up_ele if ele not in test_list]# printing resultprint("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 functoolsfrom functools import reduce# initializing settest_set = {6, 4, 2, 7, 9}# printing original listprint("The original list is : " + str(test_set))# initializing adding elementsup_ele = [1, 5, 10]# using reduce and union function to append elements to setresult_set = reduce(lambda res, ele: res.union(set([ele])), up_ele, test_set)# printing resultprint("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}
Dive into the future of technology - explore the Complete Machine Learning and Data Science Program by GeeksforGeeks and stay ahead of the curve.
Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.

