Get startedGet started for free

Sets and tuples

1. Sets and tuples

We've seen how Python allows us to store multiple values in lists and dictionaries. Let's look at more data structures for achieving this!

2. Sets

A set is a data structure that stores unique information. This means that if we have a set containing a duplicate value, then we will only see one instance of it when we print the set. Additionally, their values cannot be changed, though values can be added or deleted. Because of these properties, sets are useful for tasks such as identifying and removing duplicate values. Searching sets for values in Python is also very fast, particularly compared to other data structures such as lists.

3. Creating a set

We can make a set by using curly brackets, similar to how we make dictionaries. Python decides whether we want to make a dictionary or a set based on the inclusion of a colon. If we omit this, we create a set. Here, we make a set containing a list of attendees for an event. Even though there are two instances of John Smith, notice that when we view the variable, this name only occurs once.

4. Converting to a set

Given sets' unique properties, we might want to convert another data type, such as a list, to a set. This process is known as casting - we cast a list as a set. We can do this by calling the set function and providing our variable. Checking the data type confirms we now have a set,

5. Converting to a set

and printing it confirms there are no longer any duplicates.

6. Limitations of sets

Sets are great but have their limitations. They don't have an index. While this is a benefit because it's the reason we can't have duplicates, it's also a limitation as we can't subset them using the square brackets syntax we are familiar with. If we try, then we will get an error. We would need to use another approach, one that we will cover later in the course.

7. Sorting a set

While sets are unordered, Python has a helpful built-in function that we can apply to sort a set's values. By calling sorted and passing our attendees-set, we get the values returned in alphabetical order. Notice the output has square brackets, which is because Python converts the set to a list so it can sort the values, returning all unique values ordered alphabetically.

8. Tuples

Another Python data structure is the tuple. The key benefit of a tuple is that it is immutable, meaning it cannot be changed. We can't add, remove, or change values in a tuple, in contrast to sets where we can add or remove values, but not change them. Another strength is that tuples are ordered, just like with lists and dictionaries, which means we can subset based on the index using square brackets. Tuples are great if we need to prevent information from being edited, such as user passwords for a website.

9. Creating a tuple

We can use parentheses to make a tuple, providing as many values as we like using commas to separate them. Here, we store office locations. We can also use the tuple function to convert another data structure to a tuple.

10. Accessing tuples

We can use square brackets to subset a tuple the same way we subset a list. Here, we get the second element.

11. Data structures summary

This table provides a summary of the data structures we've learned about, along with their syntax and functionality.

12. Let's practice!

With that, you're all set to tackle some exercises!