Python – Append Multiple elements in set
In this article given a set and list of elements, the task is to write a Python program to append multiple elements in 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).
Method #1 : Using update()
In this, we use in built update() to get all the elements in list aligned with the existing set.
Python3
# Python3 code to demonstrate working of # Append Multiple elements in set # Using update() # 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}
Method #2 : Using | operator ( Pipe operator )
The pipe operator internally calls union(), which can be used to perform task of updating set with newer elements.
Python3
# Python3 code to demonstrate working of # Append Multiple elements in set # Using | operator ( Pipe operator ) # 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}
Method #3 : Using ‘|’ union operator
this function, pass in the original set as the first argument and the list of elements to be appended as the second argument. The function returns a new set that contains all the elements from both sets.
Python3
# Define a function named "append_to_set" that takes two arguments: # - test_set: a set to which new elements will be added # - up_ele: a list of new elements to be added to the test_set def append_to_set(test_set, up_ele): # Use the "|" operator to create a new set that contains all elements in both "test_set" and "up_ele" new_set = test_set | set (up_ele) # Return the new set return new_set # Create a test set with some initial elements test_set = { 6 , 4 , 2 , 7 , 9 } # Create a list of new elements to be added to the test set up_ele = [ 1 , 5 , 8 ] # Call the "append_to_set" function with the test set and list of new elements as arguments, # and assign the resulting set to a variable named "result" result = append_to_set(test_set, up_ele) # Print the resulting set print (result) |
{1, 2, 4, 5, 6, 7, 8, 9}
Time Complexity: O(n)
Auxiliary Space: O(len(test_set) + n)
Method #4:Using listcomprehension
- Initialize a set with some elements
- Define a list of elements to add to the set
- Use list comprehension to create a list of tuples where each tuple contains an element to be added to the set and a boolean value indicating whether the element is already in the set or not
- Use set comprehension to create a set of only the elements that are not already in the set, by filtering out the tuples that have True as the second element
- Use the update() method to add the filtered set of elements to the original set
- Print the updated set
Python3
# initializing list test_list = [ 6 , 4 , 2 , 7 , 9 ] # printing original list print ( "The original list 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 ( "List after adding elements : " + str ( set (test_list))) #This code is contributed by Vinay Pinjala. |
The original list is : [6, 4, 2, 7, 9] List after adding elements : {1, 2, 4, 5, 6, 7, 9, 10}
The time complexity of the code using list comprehension to append elements to a set is O(n), where n is the length of the list of elements being added to the set.
The space complexity of this code is O(n), since it creates a new list of elements that need to be added to the set, and then updates the set with these elements. The space complexity also depends on the size of the original set.
Approach#5: Using reduce
This approach uses the reduce function from the functools module to apply a union operation between each element of a list up_ele and a set test_set, resulting in a new set result_set. Finally, it prints the result_set.
Algorithm
1. Initialize the set test_set with 5 integers.
2. Initialize a list up_ele with 3 integers.
3. Use the reduce function to iterate over up_ele and apply the union operation with each element and the result_set until all the elements of up_ele are processed.
4. Print the result_set.
Python3
from functools import reduce test_set = { 6 , 4 , 2 , 7 , 9 } up_ele = [ 1 , 5 , 10 ] result_set = reduce ( lambda res, ele: res.union( set ([ele])), up_ele, test_set) print (result_set) |
{1, 2, 4, 5, 6, 7, 9, 10}
Time Complexity: O(n), where n is the length of up_ele. The reduce function iterates over each element of up_ele once and applies a set union operation, which takes constant time.
Auxiliary Space: O(n), where n is the length of up_ele. The result_set stores the union of all the elements in up_ele, which can be as large as the number of elements in up_ele. However, since the reduce function processes each element of up_ele one at a time, the space used by result_set never exceeds the size of a single element of up_ele.
Please Login to comment...