Python Tutorials

Overview

Python sets can be joined (combined) using the union(), update(), intersection(), intersection_update(), difference(), difference_update(), symmetric_difference(), and symmetric_difference_update() methods.

union() Method

The union() method returns a new set with ALL items from both sets (or any number of sets/iterables).

  • If an item is present in more than one set, the resulting NEW set will contain only one appearance of the item
  • A new set IS returned
  • The original sets remain unchanged

Example 1:

x = {"Ferrari", "Maserati", "Alfa Romeo"}
y = {"Lamborgini", "Ferrari", "Astin Martin"}

z = x.union(y) 
print(z) #{'Astin Martin', 'Alfa Romeo', 'Lamborgini', 'Maserati', 'Ferrari'}

Example 2:

x = {"Ferrari", "Maserati", "Alfa Romeo"}
y = {"Lamborgini", "Ferrari", "Astin Martin"}

z = x | y 
print(z) #{'Astin Martin', 'Alfa Romeo', 'Lamborgini', 'Maserati', 'Ferrari'}

Note

In example 2, using the (|) operator, sets can only be joined with other sets, NOT other iterables, as can be done in the first example. Using other iterables with the (|) operator will raise an error.

The following syntax is used to join multiple sets/iterables.

set.union(set1, set2, ...)

// or

set | set1 | set2 | ...

update Method()

The update() method inserts all items from one set or iterable into another. It changes the original set, but does not return a new set.

  • If an item is present in more than one set, the updated ORIGINAL set will contain only one appearance of the item
  • A new set is NOT created

Example 1:

x = {"Ferrari", "Maserati", "Alfa Romeo"}
y = {"Lamborgini", "Ferrari", "Astin Martin"}

x.update(y) 
print(x) #{'Astin Martin', 'Ferrari', 'Alfa Romeo', 'Maserati', 'Lamborgini'}

Example 2:

x = {"Ferrari", "Maserati", "Alfa Romeo"}
y = {"Lamborgini", "Ferrari", "Astin Martin"}

x |= y 
print(x) #{'Astin Martin', 'Ferrari', 'Alfa Romeo', 'Maserati', 'Lamborgini'}

Note

In example 2, using the (|=) operator, sets can only be joined with other sets, NOT other iterables, as can be done in the first example. Using other iterables with the (|=) operator will raise an error.

The following syntax is used to update multiple sets/iterables.

set.update(set1, set2, ...)

// or

set |= set1 | set2 | ...

intersection Method()

The intersection() method will return a new set that contains only the items that are present in both sets.

  • ONLY the duplicates will appear in the new set
  • A new set IS returned
  • The original sets remain unchanged

Example 1:

x = {"Ferrari", "Maserati", "Alfa Romeo"}
y = {"Lamborghini", "Maserati", "Astin Martin"}

z = x.intersection(y) 
print(z) #{'Maserati'}

Example 2:

x = {"Ferrari", "Maserati", "Alfa Romeo"}
y = {"Lamborghini", "Maserati", "Alfa Romeo"}
z = {"Maserati", "Astin Martin"}

result = x & y & z
print(result) #{'Maserati'}

Note

In example 2, using the (&) operator, sets can only be joined with other sets, NOT other iterables, as can be done in the first example. Using other iterables with the (&) operator will raise an error.

The following syntax is used to intersect multiple sets/iterables.

set.intersection(set1, set2 ... etc.)

// or

set1 & set2 & set3 ... etc.

intersection_update Method()

The intersection_update() method will keep ONLY the duplicates, but it will change the original set instead of returning a new set.

  • ONLY the duplicates will appear in the ORIGINAL set
  • A new set is NOT created

Example 1:

x = {"Ferrari", "Maserati", "Alfa Romeo"}
y = {"Lamborghini", "Maserati", "Astin Martin"}

x.intersection_update(y) 
print(x) #{'Maserati'}

Example 2:

x = {"Ferrari", "Maserati", "Alfa Romeo"}
y = {"Lamborghini", "Maserati", "Astin Martin"}

x &= y 
print(x) #{'Maserati'}

Note

In example 2, using the (&=) operator, sets can only be joined with other sets, NOT other iterables, as can be done in the first example. Using other iterables with the (&=) operator will raise an error.

The following syntax is used to intersect multiple sets/iterables.

set.intersection_update(set1, set2 ... etc)

// or

set1 &= set2 & set3 ... etc.

difference Method()

The difference() method returns a set that contains the difference between two sets.

  • The NEW set contains items that exist ONLY in the first set, and NOT in both sets
  • The original sets remain unchanged

Example 1:

x = {"Lamborghini", "Maserati", "Alfa Romeo"}
y = {"Lamborghini", "Porsche"}

z = x.difference(y) 
print(z) #{'Maserati', 'Alfa Romeo'}

Example 2:

x = {"Lamborghini", "Maserati", "Alfa Romeo"}
y = {"Lamborghini", "Porsche"}

z = x - y
print(z) #{'Maserati', 'Alfa Romeo'}

Note

In example 2, using the (-) operator, sets can only be joined with other sets, NOT other iterables, as can be done in the first example. Using other iterables with the (-) operator will raise an error.

The following syntax is used to find the difference in multiple sets/iterables.

set.difference(set1, set2 ... etc.)

// or

set1 - set2 - set3 ... etc.

difference_update Method()

The difference_update() method removes the items that exist in both sets.

  • The original set will be changed and ONLY the duplicates will remain in the ORIGINAL set
  • A new set is NOT created

Example 1:

x = {"Ferrari", "Maserati", "Alfa Romeo"}
y = {"Lamborghini", "Ferrari"}

x.difference_update(y) 
print(x) #{'Alfa Romeo', 'Maserati'}

Example 2:

x = {"Ferrari", "Maserati", "Alfa Romeo"}
y = {"Lamborghini", "Ferrari"}

x -= y 
print(x) #{'Alfa Romeo', 'Maserati'}

Note

In example 2, using the (-=) operator, sets can only be joined with other sets, NOT other iterables, as can be done in the first example. Using other iterables with the (-=) operator will raise an error.

The following syntax is used to find the difference in multiple sets/iterables.

set.difference_update(set1, set2 ... etc.)

// or

set1 -= set2 | set3 ... etc.

symmetric_difference Method()

The symmetric_difference() method will keep only the elements that are NOT present in both sets.

  • The NEW set contains items that are NOT present in both original sets
  • The original sets remain unchanged

Example 1:

x = {"Ferrari", "Maserati", "Alfa Romeo"}
y = {"Lamborghini", "Maserati", "Porsche", "Ferrari", "Astin Martin", "Alfa Romeo"}

z = x.symmetric_difference(y) 
print(z) #{'Porsche', 'Astin Martin', 'Lamborghini'}

Example 2:

x = {"Ferrari", "Maserati", "Alfa Romeo"}
y = {"Lamborghini", "Maserati", "Porsche", "Ferrari", "Astin Martin", "Alfa Romeo"}

z = x ^ y 
print(z) #{'Porsche', 'Astin Martin', 'Lamborghini'}

Note

In example 2, using the (^) operator, sets can only be joined with other sets, NOT other iterables, as can be done in the first example. Using other iterables with the (^) operator will raise an error.

The following syntax is used to find the difference in multiple sets/iterables.

set.symmetric_difference(set1)

// or

set ^ set1

symmetric_difference_update Method()

The symmetric_difference_update() method will also keep only the elements that are NOT present in both sets. But it will not create a new set.

  • The original set will be changed and ONLY the items not present in both sets will remain in the ORIGINAL set
  • A new set is NOT created

Example 1:

x = {"Ferrari", "Maserati", "Alfa Romeo"}
y = {"Lamborghini", "Maserati", "Porsche","Astin Martin", "Alfa Romeo"}

x.symmetric_difference_update(y) 
print(x) #{'Astin Martin', 'Porsche', 'Ferrari', 'Lamborghini'}

Example 2:

x = {"Ferrari", "Maserati", "Alfa Romeo"}
y = {"Lamborghini", "Maserati", "Porsche","Astin Martin", "Alfa Romeo"}

x ^= y
print(x) #{'Astin Martin', 'Porsche', 'Ferrari', 'Lamborghini'}

Note

In example 2, using the (^=) operator, sets can only be joined with other sets, NOT other iterables, as can be done in the first example. Using other iterables with the (^=) operator will raise an error.

The following syntax is used to find the difference in multiple sets/iterables.

set.symmetric_difference_update(set1)

// or

set ^= set1

Python Notes:

  • The most recent major version of Python is Python 3; however, Python 2 is still in use and quite popular, although not being updated with anything other than security updates
  • Python uses new lines to complete a command, as opposed to other programming languages which often use semicolons or parentheses
  • Python relies on indentation, using whitespace to define scope, such as the scope of loops, functions, and classes; other programming languages often use curly-brackets for this purpose
  • Python string methods return new values, and DO NOT change the original string
  • Python tuples are unchangeable after created (their items CANNOT be changed or re-ordered at a later point)
  • Python sets are unordered (may appear in random orders when called), unchangeable (the value of individual items cannot be changed after creation), unindexed (items cannot be referred to by index or key), and duplicates are NOT ALLOWED
  • As of v3.7, Python dictionaries are ordered and duplicates ARE ALLOWED; in v3.6 and earlier, dictionaries were unordered (did not have a defined order and could not be referred to using an index)

We’d like to acknowledge that we learned a great deal of our coding from W3Schools and TutorialsPoint, borrowing heavily from their teaching process and excellent code examples. We highly recommend both sites to deepen your experience, and further your coding journey. We’re just hitting the basics here at 1SMARTchicken.