The set data structure is one of the most underused primary data structures in Python. I’ve seen so many intermediate-level coders who use lists when all they need is a data structure that checks membership!! ? Understanding the basics differentiates the good from the great. In this tutorial, you’ll learn about Python’s basic set methods.

First, I’ll give you a short tabular overview of all set methods. Second, I’ll teach you every single set method in a short 5-10 minute video. You can see this as a full-fledged Python set methods course—the only one I’m aware of on the web.

You can download the set methods in a concise PDF here:

The course was brought to you by Finxter Academy. If you want to bring your Python programming skills to the point where you can create your own successful coding projects and earn money as a Python freelancer, check out the Finxter Computer Science Academy (opens in a new tab). It’s inexpensive but provides huge value for your career as a programmer! ?

## Python Set Methods Quick Overview

All set methods are called on a given set. For example, if you created a set `s = {1, 2, 3}`

, you’d call `s.clear()`

to remove all elements of the set. We use the term ** “this set”** to refer to the set on which the method is executed.

`add()` | Add an element to this set |

`clear()` | Remove all elements from this set |

`copy()` | Create and return a flat copy of this set |

`difference()` | Create and return a new set containing all elements of this set except the ones in the given set arguments. The resulting set has at most as many elements as this set. |

`difference_update()` | Remove all elements from this set that are members of any of the given set arguments. |

`discard()` | Remove an element from this set if it is a member, otherwise do nothing. |

`intersection()` | Create and return a new set that contains all elements that are members of all sets — this and the set argument(s) as well. |

`intersection_update()` | Removes all elements from this set that are not members in all other specified sets. |

`isdisjoint()` | Return `True` if no element from this set is a member of any other specified set. Sets are disjoint if and only if their intersection is the empty set. |

`issubset()` | Return `True` if all elements of this set are members of the specified set argument. |

`issuperset()` | Return `True` if all elements of the specified set argument are members of this set. |

`pop()` | Remove and return a random element from this set. If the set is empty, it’ll raise a `KeyError` . |

`remove()` | Remove and return a specific element from this set as defined in the argument. If the set doesn’t contain the element, it’ll raise a `KeyError` . |

`symmetric_difference()` | Return a new set with elements in either this set or the specified set argument, but not elements that are members of both. |

`symmetric_difference_update()` | Replace this set with the symmetric difference, i.e., elements in either this set or the specified set argument, but not elements that are members of both. |

`union()` | Create and return a new set with all elements that are in this set, or in any of the specified set arguments. |

`update()` | Update this set with all elements that are in this set, or in any of the specified set arguments. The resulting set has at least as many elements as any other. |

## Python Set Methods

Let’s dive into the set methods one by one—each comes with a short and concise **overview**, a **full video** tutorial recorded by me, and a **minimal example**. That’s all you need to master the set methods once and for all! ?

### set.add()

Python’s `set.add(element)`

method adds the `element`

to this `set`

. If the element is already present in the set, the method returns without any side effect.

Here’s a minimal example where we add element 4 to an existing set:

>>> s = {1, 2, 3} >>> s.add(4) >>> s {1, 2, 3, 4}

You can read the full guide on the method here.

### set.clear()

Python’s `set.clear()`

method removes all elements from this set. All variables that refer to this set object will refer to an empty set after calling the method.

Here’s a minimal example where you remove three elements from a set at once by means of the `s.clear()`

method:

>>> s = {1, 2, 3} >>> s.clear() >>> s set()

You can read the full guide on the method here.

### set.copy()

Python’s `set.copy()`

method creates and returns a flat copy of this set.

Here’s a minimal example where you copy a set with two integers and a string value:

>>> s = {1, 2, 'Alice'} >>> s.copy() {1, 2, 'Alice'}

You can read the full guide on the method here.

### set.difference()

Python’s `set.difference(sets)`

method creates and returns a new set containing all elements of this `set`

, except the ones in the given set argument or arguments. The resulting set has at most as many elements as this set.

Here’s a minimal example where we return a new set with the elements from an existing set after removing the elements 1 and 2 from the new set:

>>> s = {1, 2, 3} >>> t = {1, 2} >>> s.difference(t) {3}

You can read the full guide on the method here.

### set.difference_update()

Python’s `set.difference_update(*args)`

method removes all elements from this set that are members of any of the given set arguments. For example, `s.difference_update({1, 2})`

removes elements 1 and 2 from the set `s`

. Its return value is `None`

because it modifies the set it is called upon rather than creating a new set.

Here’s a minimal example where we remove elements 1 and 2 from an existing set:

>>> s = {1, 2, 3} >>> s.difference_update({1, 2}) >>> s {3}

You can read the full guide on the method here.

### set.discard()

Python’s `set.discard(x)`

method removes an element `x`

from this set if it is a member. Otherwise does nothing.

Here’s a minimal example where you remove the string element `'Bob'`

from the set by means of the `s.discard()`

method:

>>> s = {'Alice', 'Bob', 'Cloe'} >>> s.discard('Bob') >>> s {'Alice', 'Cloe'}

You can read the full guide on the method here.

### set.intersection()

>>> s = {1, 2, 3, 4} >>> t = {3, 4, 5} >>> s.intersection(t) {3, 4}

You can read the full guide on the method here.

### set.intersection_update()

>>> s = {1, 2, 3, 4} >>> t = {3, 4, 5} >>> s.intersection_update(t) >>> s {3, 4}

You can read the full guide on the method here.

### set.isdisjoint()

>>> s = {1, 2, 3, 4} >>> t = {'Alice', 'Bob'} >>> s.isdisjoint(t) True

You can read the full guide on the method here.

### set.issubset()

>>> s = {'Alice', 'Bob'} >>> t = {'Alice', 'Bob', 'Carl', 'Liz'} >>> s.issubset(t) True

You can read the full guide on the method here.

### set.issuperset()

>>> s = {'Alice', 'Bob', 'Carl'} >>> t = {'Alice', 'Bob'} >>> s.issuperset(t) True

You can read the full guide on the method here.

### set.pop()

Python’s `set.pop()`

method removes and returns an arbitrary element from the set. If no element is present — that is, you call it on an empty set — `set.pop()`

raises a `KeyError`

.

Here’s a minimal example where you remove a string element that happens to be `'Alice'`

from the set by means of the `s.pop()`

method:

>>> s = {'Alice', 'Bob', 'Carl'} >>> s.pop() 'Alice'

You can read the full guide on the method here.

### set.remove()

Python’s `set.remove(x)`

method removes an element `x`

from this set if it is a member, otherwise it raises a `KeyError`

.

Here’s a minimal example where you remove the string element `'Bob'`

from the set by means of the `s.remove()`

method:

>>> s = {'Alice', 'Bob', 'Cloe'} >>> s.remove('Bob') >>> s {'Alice', 'Cloe'}

You can read the full guide on the method here.

### set.symmetric_difference()

Python’s `S.symmetric_difference(T)`

method creates and returns a new set containing all elements that are in exactly one of the two sets `S`

and `T`

.

Here’s a minimal example where we return a new set containing the elements 1 and 4 that are in exactly one of the two sets `s`

and `t`

.

>>> s = {1, 2, 3} >>> t = {2, 3, 4} >>> s.symmetric_difference(t) {1, 4}

You can read the full guide on the method here.

### set.symmetric_difference_update()

Python’s `S.symmetric_difference_update(T)`

determines all elements that are in exactly one of the two sets `S`

or `T`

and updates set `S`

with those elements.

Here’s a minimal example where we update an existing set to consist of elements 1 and 4 that are in exactly one of the two sets `s`

and `t`

.

>>> s = {1, 2, 3} >>> t = {2, 3, 4} >>> s.symmetric_difference_update(t) >>> s {1, 4}

You can read the full guide on the method here.

### set.union()

>>> s = {1, 2, 3, 4} >>> t = {3, 4, 5} >>> s.union(t) {1, 2, 3, 4, 5}

You can read the full guide on the method here.

### set.update()

>>> s = {1, 2, 3, 4} >>> t = {3, 4, 5} >>> s.update(t) >>> s {1, 2, 3, 4, 5}

You can read the full guide on the method here.

## Where to Go From Here?

Thanks for reading and watching over this whole tutorial! ? It took me a month to record these videos but if you learned something new today, it was all worth it! If you want to boost your programming skills and reach more success with your own 1-person coding business, feel free to check out the other content on the Finxter Computer Science Academy with a wide variety of courses on Python lists, tricks, PyCharm, NumPy, machine learning, and many more exciting areas in computer science. The costs are very affordable but the value for your life will be enormous. Have a look at our selection of premium courses here:

While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.

To help students reach higher levels of Python success, he founded the programming education website Finxter.com. He’s author of the popular programming book Python One-Liners (NoStarch 2020), coauthor of the Coffee Break Python series of self-published books, computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.

His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.