Python Set update()

The set.update() method in Python is used to update a set with the union of elements to an existing set. You can update elements from any iterable object like a list, tuple, or another set. Let’s discuss this method in this article.

1. Quick Examples of Set update()

Following are quick examples of how to use the update() method to add elements to the set.


# Consider the set with some integers
myset = {12,34,56,3,45,67,89,1,6}
print("Actual set:",myset)

# Use update() method to add elements from a list
myset.update(["Hello","welcome"])
print("Set after adding list of elements:",myset)

# Use update() method to add elements from three tuples
myset.update((0,0),(5,6),(4,5))
print("Set after adding tuple of elements:",myset)

# Use update() method to add elements from a set
myset.update({"sparkby","examples"})
print("Set after adding set of elements:",myset)

# Use update() method to add a character
myset.update('J')
print("Set after adding a character:",myset)

# Use update() method to add elements from a dictionary
myset.update({"Hello":1,"welcome":2})
print("Set after adding dictionary keys:",myset)

2. Set update() in Python

The set.update() method is used to update a set with an iterable object like a list, tuple, or another set. So we can pass any iterable to it as an argument. All the elements present in these iterables get added to the set. But if the elements are already contained in the set, then the set won’t include them.

2.1 Syntax of Set update()

Following is the syntax of the update() method.


# Syntax of update
setObj.update(iterable/s)

2.2 Parameters of Set update()

Here, it will take single or multiple iterables at a time.

3 Update Set From the List

Since update() method takes the iterable objects as input, let’s use the list as input to the update(). This basically unions all the elements from the input to the original set object.

Keep in mind that set.update() is a mutating operation, which means that it modifies the original set in place. If you don’t want to modify the original set, you can use the union() method to create a new set which is the union of two sets.


# Consider the set with some integers
myset = {12,34,56,3,45,67,89,1,6}
print("Actual set:",myset)

# Use update() method to add elements from a list
myset.update(["Hello","welcome"])
print("After Update:",myset)

# Use update elements from two lists
myset.update([0,0],[5,6])
print("After Update:",myset)

# Output:
# Actual set: {1, 34, 3, 67, 6, 12, 45, 56, 89}
# After Update: {1, 34, 3, 67, 6, 'welcome', 'Hello', 12, 45, 56, 89}
# After Update: {0, 1, 34, 3, 67, 5, 6, 'welcome', 'Hello', 12, 45, 56, 89}

First, we added 2 elements from a list to the set, Next, we added 4 elements from two lists to the set using update() method. Now the final set contains – {0, 1, 34, 3, 67, 5, 6, 'welcome', 'Hello', 12, 45, 56, 89}.

4. Update Set from Tuple

Let’s see another example of updating a set with elements from the tuple.


# Consider the set with some integers
myset = {12,34,56,3,45,67,89,1,6}
print("Actual set:",myset)

# Use update() method to add elements from a tuple
myset.update(("Hello","welcome"))
print("After Update:",myset)

# Use update() method to add elements from three tuples
myset.update((0,0),(5,6),(4,5))
print("After Update:",myset)

# Output:
# Actual set: {1, 34, 3, 67, 6, 12, 45, 56, 89}
# After Update: {1, 34, 3, 67, 6, 'welcome', 'Hello', 12, 45, 56, 89}
# After Update: {0, 1, 34, 3, 67, 5, 6, 4, 'welcome', 'Hello', 12, 45, 56, 89}

First, we added 2 elements from a tuple to the set, Next, we are adding 3 tuples to the set that include duplicates. The final set holds – {0, 1, 34, 3, 67, 5, 6, 4, 'welcome', 'Hello', 12, 45, 56, 89}.

5. Update from another Set

Let’s pass set of elements to the update() method.


# Consider the set with some integers
myset = {12,34,56,3,45,67,89,1,6}
print("Actual set:",myset)

# Use update() method to add elements from a set
myset.update({"Hello","welcome"})
print("After Update:",myset)

# Use update() method to add elements from a set
myset.update({"Hello","welcome"})
print("After Update:",myset)

# Output:
# Actual set: {1, 34, 3, 67, 6, 12, 45, 56, 89}
# After Update: {1, 34, 3, 67, 6, 'welcome', 'Hello', 12, 45, 56, 89}
# After Update: {1, 34, 3, 67, 6, 'welcome', 'Hello', 12, 45, 56, 89}

We are adding elements from a set. In the second update(), we passed the same elements, Set won’t add these because they are duplicates. The final set hold – {1, 34, 3, 67, 6, 'welcome', 'Hello', 12, 45, 56, 89}.

6. Updating from Dictionary

Let’s pass the dictionary to the update() method. In this scenario, only keys from the dictionary will get added to the set.


# Consider the set with some integers
myset = {12,34,56,3,45,67,89,1,6}
print("Actual set:",myset)

# Use update() method to add elements from a dictionary
myset.update({"Hello":1,"welcome":2})
print("Set after adding dictionary keys:",myset)

# Output:
# Actual set: {1, 34, 3, 67, 6, 12, 45, 56, 89}
# Set after adding dictionary keys: {1, 34, 3, 67, 6, 'welcome', 'Hello', 12, 45, 56, 89}

The keys in the dictionary are “Hello” and “welcome“. So only these are added to the existing set.

9. Update the Character to the Set

Let’s add a single character ‘J’ to the set.


# Consider the set with some integers
myset = {12,34,56,3,45,67,89,1,6}
print("Actual set:",myset)

# Use update() method to add a character
myset.update('J')
print("Set after adding a character:",myset)

# Output:
# Actual set: {1, 34, 3, 67, 6, 12, 45, 56, 89}
# Set after adding a character: {1, 34, 3, 67, 6, 12, 45, 56, 89, 'J'}

The final set after adding character 'J' is {1, 34, 3, 67, 6, 12, 45, 56, 89, 'J'}.

10. Conclusion

We discussed how to update multiple elements at a time to the set using the update() method in Python. The Set update() method is used to add single or multiple elements at a time. So we can pass any iterable like Set, list, tuple or dictionary to it.

Leave a Reply