Difference_update Method Of Set In Python

Overview:

  • The method difference_update() removes elements of a set as specified by the another set.
  • In other words, the difference between the original set and another set is calculated, which is updated as the contents of the original set.

Example:

# Example Python program that removes 
# the elements from a set as specified by another set 
brass   = {"Trumpet", "Bugle", "French Horn", "Bass Trambone", "Piccola Trumpet", "Tuba"}
remove  = {"Piccola Trumpet"}
print("Brass instruments:")
print(brass)

# Remove a set of instruments
brass.difference_update(remove)

# Print the new set
print("New brass instruments:")
print(brass)

Output:

Brass instruments:

{'Tuba', 'Piccola Trumpet', 'Bugle', 'Trumpet', 'Bass Trambone', 'French Horn'}

New brass instruments:

{'Tuba', 'Bugle', 'Trumpet', 'Bass Trambone', 'French Horn'}

 


Copyright 2023 © pythontic.com