Python Set update() Method with Examples

Prerequisite:

Python set() Function with Examples

Set update() Method in Python:

The update() method adds items from another set to the current set (or any other iterable).

If an item appears in both sets, only one appearance will appear in the updated set.

Syntax:

set.update(set)

Parameters

set: This is Required. It is iterable to be inserted into the current set.

Return Value:

None is returned by the set update() method (returns nothing).

Examples:

Example1:

Input:

Given first set =  {'a', 'b', 'c'}
Given second set = {'b', 'p', 'q'}

Output:

The given first set after updating is : {'p', 'q', 'b', 'a', 'c'}
The given second set is : {'p', 'q', 'b'}
The result after applying the update() method:  None

Explanation:

Here, add items of the second set to the first set and update it.
But 'b' appears in both sets, only one appearance of 'b' will appear in the 
updated first set.

Example2:

Input:

Given first set = {9, 11, 2, 6}
Given second set = {2, 1, 3, 9}

Output:

The given first set is : {9, 2, 11, 6}
The given second set after updating is : {1, 2, 3, 6, 9, 11}
The result after applying the update() method: None

Set update() Method with Examples in Python

Method #1: Using Built-in Functions (Static Input)

Approach:

  • Give the first set as static input and store it in a variable.
  • Give the second set as static input and store it in another variable.
  • Apply the update() method to the given first and second sets.
  • Store it in another variable.
  • Print the given first set.
  • Print the given second set.
  • Print the above result after applying the update() method for the given two sets.
  • The Exit of the Program.

Below is the implementation:

# Give the first set as static input and store it in a variable.
fst_set = {'a', 'b', 'c'}
# Give the second set as static input and store it in another variable.
scnd_set = {'b', 'p', 'q'}
# Apply the update() method to the given first and second sets.
# Store it in another variable.
rslt = fst_set.update(scnd_set)
# print the given first set.
print("The given first set after updating is :", fst_set)
# print the given second set.
print("The given second set is :", scnd_set)
# Print the above result after applying the update() method for the
# given two sets.
print("The result after applying the update() method: ", rslt)

Output:

The given first set after updating is : {'p', 'q', 'b', 'a', 'c'}
The given second set is : {'p', 'q', 'b'}
The result after applying the update() method:  None

Method #2: Using Built-in Functions (User Input)

Approach:

  • Give the first set as user input using set(),map(),input(),and split() functions.
  • Store it in a variable.
  • Give the second set as user input using set(),map(),input(),and split() functions.
  • Store it in another variable.
  • Apply the update() method to the given first and second sets.
  • Store it in another variable.
  • Print the given first set.
  • Print the given second set.
  • Print the above result after applying the update() method for the given two sets.
  • The Exit of the Program.

Below is the implementation:

# Give the first set as user input using set(),map(),input(),and split() functions.
# Store it in a variable.
fst_set = set(map(int, input(
   'Enter some random Set Elements separated by spaces = ').split()))
# Give the second set as user input using set(),map(),input(),and split() functions.
# Store it in another variable.
scnd_set = set(map(int, input(
   'Enter some random Set Elements separated by spaces = ').split()))

# Apply the update() method to the given first and second sets.
# Store it in another variable.
rslt = scnd_set.update(fst_set)
# print the given first set.
print("The given first set is :", fst_set)
# print the given second set.
print("The given second set after updating is :", scnd_set)
# Print the above result after applying the update() method for the
# given two sets.
print("The result after applying the update() method: ", rslt)

Output:

Enter some random Set Elements separated by spaces = 9 11 2 6
Enter some random Set Elements separated by spaces = 2 1 3 9
The given first set is : {9, 2, 11, 6}
The given second set after updating is : {1, 2, 3, 6, 9, 11}
The result after applying the update() method: None