Python - Append Multiple elements in set
Last Updated :
05 Jul, 2023
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}
Similar Reads
Sets in Python
A Set in Python is used to store a collection of items with the following properties.No duplicate elements. If try to insert the same item again, it overwrites previous one.An unordered collection. When we access all items, they are accessed without any specific order and we cannot access items usin
9 min read
Python Sets
Python set is an unordered collection of multiple items having different datatypes. In Python, sets are mutable, unindexed and do not contain duplicates. The order of elements in a set is not preserved and can change.Creating a Set in PythonIn Python, the most basic and efficient method for creating
11 min read
Internal working of Set in Python
A Set in Python can be defined as a collection of unique items. It is an unordered collection that does not allow duplicate entries. Sets are primarily used for membership testing and eliminating duplicates from a sequence. The underlying data structure used in sets is Hashing, which allows insertio
4 min read
Find the length of a set in Python
We are given a set and our task is to find its length. For example, if we have a = {1, 2, 3, 4, 5}, the length of the set should be 5.Using len()len() function in Python returns the number of elements in a set. It provides total count of unique items present in set.Pythona = {1, 2, 3, 4, 5} # Return
2 min read
Checking Element Existence in a Python Set
We are given a set and our task is to check if a specific element exists in the given set. For example, if we have s = {21, 24, 67, -31, 50, 11} and we check for the element 21, which is present in the set then the output should be True. Let's explore different ways to perform this check efficiently
2 min read
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}Expla
4 min read
Remove items from Set - Python
We are given a set and our task is to remove specific items from the given set. For example, if we have a = {1, 2, 3, 4, 5} and need to remove 3, the resultant set should be {1, 2, 4, 5}.Using remove()remove() method in Python is used to remove a specific item from a set. If the item is not present
2 min read
Python - Remove multiple elements from Set
Given a set, the task is to write a Python program remove multiple elements from set. Example: Input : test_set = {6, 4, 2, 7, 9}, rem_ele = [2, 4, 8] Output : {9, 6, 7} Explanation : 2, 4 are removed from set. Input : test_set = {6, 4, 2, 7, 9}, rem_ele = [4, 8] Output : {2, 9, 6, 7} Explanation :
4 min read
Python program to remove last element from set
Given a set, the task is to write a Python program to delete the last element from the set. Example: Input: {1,2,3,4}Remove 4Output: {1,2,3} Input: {"Geeks","For"}Remove GeeksOutput: {"For"}Explanation: The idea is to remove the last element from the set 4 in the first case and "Geeks" in the second
2 min read
Python Program to Find Duplicate sets in list of sets
Given a list of sets, the task is to write a Python program to find duplicate sets. Input : test_list = [{4, 5, 6, 1}, {6, 4, 1, 5}, {1, 3, 4, 3}, {1, 4, 3}, {7, 8, 9}]Output : [frozenset({1, 4, 5, 6}), frozenset({1, 3, 4})]Explanation : {1, 4, 5, 6} is similar to {6, 4, 1, 5} hence part of result.
8 min read