Working with Sets and Frozen Sets in Python
Tags: python, beginners, programming
Published: 2024-02-18 23:57:52

In this post, we'll explore sets and frozen sets in Python, using an astronomy theme to provide code examples. Sets are unordered collections of unique elements, while frozen sets are immutable versions of sets. Let's get started.

First, let's create a set of planets in our solar system:

```{python}
planets = {"Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune"}
print(planets)

# Output: {'Venus', 'Jupiter', 'Earth', 'Mars', 'Saturn', 'Neptune', 'Uranus', 'Mercury'}
```

In this example, we create a set called `planets` containing the names of the eight planets in our solar system. Since sets are unordered, the elements are not displayed in any particular order.

We can add elements to a set using the add method:

```{python}
planets.add("Pluto")
print(planets)

# Output: {'Venus', 'Jupiter', 'Earth', 'Mars', 'Saturn', 'Neptune', 'Uranus', 'Mercury', 'Pluto'}
```

In this example, we add the dwarf planet Pluto to our set of planets using the `add` method.

We can remove elements from a set using the `remove` method or the `discard` method. The `remove` method raises an error if the element is not found, while the `discard` method does not:

```{python}
planets.remove("Pluto")
print(planets)

# Output: {'Venus', 'Jupiter', 'Earth', 'Mars', 'Saturn', 'Neptune', 'Uranus', 'Mercury'}
```

```{python}
planets.discard("Pluto")
print(planets)

# Output: {'Venus', 'Jupiter', 'Earth', 'Mars', 'Saturn', 'Neptune', 'Uranus', 'Mercury'}
```

In this example, we remove Pluto from our set of planets using the `remove` method. Since Pluto is no longer in the set, calling the `discard` method with "Pluto" as an argument has no effect.

We can also remove and return an arbitrary element from a set using the `pop` method:

```{python}
planet = planets.pop()
print(planet)

# Output: Venus
```

In this example, we remove and return an arbitrary element from our set of planets using the `pop` method. Since sets are unordered, the element returned by the `pop` method is not predictable.

We can clear all elements from a set using the clear method:

```{python}
planets.clear()
print(planets)

# Output: set()
```

In this example, we remove all elements from our set of planets using the `clear` method.

We can perform set operations such as union, intersection, and difference using the `union`, `intersection`, and `difference` methods, respectively:

```{python}
planets = {"Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune"}
gas_giants = {"Jupiter", "Saturn", "Uranus", "Neptune"}

inner_planets = planets.difference(gas_giants)
print(inner_planets)

# Output: {'Venus', 'Earth', 'Mars', 'Mercury'}
```

```{python}
outer_planets = planets.intersection(gas_giants)
print(outer_planets)

# Output: {'Jupiter', 'Saturn', 'Neptune', 'Uranus'}
```

In this example, we create a set of gas giants and use the `difference` and `intersection` methods to find the inner and outer planets, respectively.

We can also check if a set is a subset or superset of another set using the `issubset` and `issuperset` methods, respectively:

```{python}
print(gas_giants.issubset(planets))

# Output: True
```

```{python}
print(planets.issuperset(gas_giants))

# Output: True
```

In this example, we use the `issubset` and `issuperset` methods to check if the set of gas giants is a subset of the set of planets, and if the set of planets is a superset of the set of gas giants, respectively.

We can also check if two sets are disjoint (have no elements in common) using the `isdisjoint` method:

```{python}
rocky_planets = {"Mercury", "Venus", "Earth", "Mars"}
print(gas_giants.isdisjoint(rocky_planets))

# Output: True
```

In this example, we create a set of rocky planets and use the `isdisjoint` method to check if the set of gas giants and the set of rocky planets have no elements in common.

We can also create a shallow copy of a set using the `copy` method:

```{python}
planets_copy = planets.copy()
print(planets_copy)

# Output: {'Venus', 'Jupiter', 'Earth', 'Mars', 'Saturn', 'Neptune', 'Uranus', 'Mercury'}
```

In this example, we create a shallow copy of our set of planets using the `copy` method.

In addition to sets, Python also has a built-in `frozenset` type, which is an immutable version of a set. This means that, once created, a frozenset cannot be modified. We can create a frozenset using the `frozenset` function:

```{python}
frozen_gas_giants = frozenset(gas_giants)
print(frozen_gas_giants)

# Output: frozenset({'Jupiter', 'Saturn', 'Neptune', 'Uranus'})
```

In this example, we create a `frozenset` of gas giants using the frozenset function. Since frozensets are immutable, we cannot add or remove elements from them.

That concludes our introduction to sets and frozen sets in Python. We've covered the basics of creating and accessing sets and frozen sets, as well as some of their useful methods and attributes. Sets and frozen sets are powerful tools for working with collections of unique elements, and can be a useful addition to your Python toolkit.