Back to Course

Intermediate Python

0% Complete
0/0 Steps

In Python, tuples and sets are two data structures that are similar to lists, but with some important differences. Understanding how to work with tuples and sets is crucial for any intermediate Python programmer. In this article, we’ll explore the key features and differences of tuples and sets, and how to use them effectively in your Python programs.

Tuples

First, let’s take a look at tuples. Tuples are immutable sequences of values, which means that once you create a tuple, you can’t change its values. You can create a tuple by enclosing a comma-separated list of values in parentheses. For example:

# Create a tuple
t = (1, 2, 3)

print(t)  # Output: (1, 2, 3)

You can also create a tuple with a single value by including a trailing comma. For example:

# Create a tuple with a single value
t = (1,)

print(t)  # Output: (1,)

You can access the values of a tuple using the indexing and slicing operators, just like with a list. For example:

t = (1, 2, 3, 4, 5)

# Access the first value
print(t[0])  # Output: 1

# Access the last value
print(t[-1])  # Output: 5

# Access a slice of values
print(t[1:3])  # Output: (2, 3)

However, unlike lists, you can’t modify the values of a tuple. If you try to assign a new value to an element of a tuple, you’ll get a TypeError. For example:

t = (1, 2, 3)

# This will cause a TypeError
t[0] = 4

Tuples have some useful features that make them useful in certain situations. For example, you can use a tuple as a key in a dictionary, or as an element in a set, because tuples are immutable and therefore hashable. You can also use a tuple to return multiple values from a function. For example:

def divide(x, y):
    quotient = x // y
    remainder = x % y
    return quotient, remainder

# Use a tuple to return multiple values from a function
quotient, remainder = divide(10, 3)

print(quotient)  # Output: 3
print(remainder)  # Output: 1

Sets

Now let’s take a look at sets. Sets are unordered collections of unique elements, which means that each element can only appear once in a set, and the order of the elements is not important. You can create a set by enclosing a comma-separated list of values in curly braces. For example:

# Create a set
s = {1, 2, 3}

print(s)  # Output: {1, 2, 3}

You can add elements to a set using the add() method, or remove elements using the remove() or discard() methods. For example:

s = {1, 2, 3}

# Add an element
s.add(4)

# Remove an element
s.remove(3)

# Remove an element that doesn't exist
s.discard(5)
print(s)  # Output: {1, 2, 4}

You can also use the union(), intersection(), and difference() methods to compute the union, intersection, and difference of two sets, respectively. For example:

s1 = {1, 2, 3}
s2 = {2, 3, 4}

# Union of two sets
s3 = s1.union(s2)

# Intersection of two sets
s4 = s1.intersection(s2)

# Difference of two sets
s5 = s1.difference(s2)

print(s3)  # Output: {1, 2, 3, 4}
print(s4)  # Output: {2, 3}
print(s5)  # Output: {1}

Sets also have some useful features for testing membership, such as the in operator and the issubset() and issuperset() methods. For example:

s1 = {1, 2, 3}
s2 = {2, 3, 4}

# Test membership with the in operator
print(2 in s1)  # Output: True
print(2 in s2)  # Output: True

# Test if a set is a subset of another set
print(s1.issubset(s2))  # Output: False
print(s1.issubset({1, 2, 3, 4}))  # Output: True

# Test if a set is a superset of another set
print(s2.issuperset(s1)) # Output: False
print({1, 2, 3, 4}.issuperset(s1)) # Output: True

Conclusion

Tuples and sets are useful data structures to have in your toolkit as an intermediate Python programmer. While they may not be as versatile as lists, they have their own unique features and use cases that can make them valuable in certain situations. By understanding how to work with tuples and sets, you’ll be able to write more efficient and powerful Python code.

Exercises

To review these concepts, we will go through a series of exercises designed to test your understanding and apply what you have learned.

Write a function that takes a list as an argument and returns a tuple with the minimum and maximum values of the list.

def min_max(lst):
    return min(lst), max(lst)

print(min_max([1, 2, 3, 4, 5]))  # Output: (1, 5)
print(min_max([-1, -2, -3, -4, -5]))  # Output: (-5, -1)

Write a function that takes a list as an argument and returns a set with the unique elements of the list.

def unique(lst):
    return set(lst)

print(unique([1, 2, 3, 4, 5]))  # Output: {1, 2, 3, 4, 5}
print(unique([1, 2, 3, 4, 5, 1, 2, 3]))  # Output: {1, 2, 3, 4, 5}

Write a function that takes two sets as arguments and returns a new set with the elements that are present in both sets.

def intersection(s1, s2):
    return s1.intersection(s2)

print(intersection({1, 2, 3}, {2, 3, 4}))  # Output: {2, 3}
print(intersection({1, 2, 3}, {4, 5, 6}))  # Output: set()

Write a function that takes a set as an argument and returns a tuple with the elements of the set sorted in ascending order.

def sort(s):
    return tuple(sorted(s))

print(sort({3, 2, 1}))  # Output: (1, 2, 3)
print(sort({5, 4, 3, 2, 1}))  # Output: (1, 2, 3, 4, 5)

Write a function that takes a set as an argument and returns a tuple with the elements of the set sorted in descending order.

def sort_descending(s):
    return tuple(sorted(s, reverse=True))

print(sort_descending({3, 2, 1}))  # Output: (3, 2, 1)
print(sort_descending({5, 4, 3, 2, 1}))  # Output: (5, 4, 3, 2, 1)