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 ideal for de-duplicating data, or tracking unique items. For example, in our recipe scaler, we might use a set to track unique ingredients across multiple recipes. Searching sets for values in Python is also very fast, particularly compared to other data structures such as lists where each element would need to be checked.3. Creating a set
We create a set using curly brackets, similar to dictionaries. Python determines whether we're creating a dictionary or a set based on the presence of a colon. Without colons, we create a set. Here, we make a set containing our ingredients. Even though there are two instances of pasta, notice that when we view the variable, this ingredient 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 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 also means we can't access elements by position like we do with lists. If we try, then we will get an error.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 ingredients_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 to enable sorting, returning all unique values in alphabetical order.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. Tuples are also ordered, just like lists and dictionaries, which means we can access elements by index using square brackets. Tuples are great if we need to prevent information from being edited, ensuring data integrity, such as for location information or identifiers.9. Creating a tuple
We create a tuple using parentheses, providing values separated by commas. Here, we store serving sizes. We can also use the tuple function to convert another data structure to a tuple.10. Accessing tuples
We can use square brackets to access tuple elements the same way we access list elements. Here, we get the second element.11. Let's practice!
With that, you're all set to tackle some exercises!Create Your Free Account
or
By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.