From the course: Python Essential Training

Tuples and sets - Python Tutorial

From the course: Python Essential Training

Tuples and sets

- [Instructor] Alright, let's take a deeper dive into two data structures we've covered only briefly before. We're talking about tuples and sets. But first, let's talk about sets. Remember, a set is declared using curly brackets, like so, {'a', 'b', 'c'}, mySet. There we go, it has those curly brackets. You can also define it by passing any sort of iterable object in the constructor of the set class. So mySet, and here we have a set object constructor. And then you can do, {'a', 'b', 'c'}, same exact thing or you could even use a tuple in here. So here we're passing a tuple with A, B, and C in there and we still get the same set. One common pattern in programming is to remove duplicates from a list. Well, because you can convert a list to a set and then back again and sets can only contain unique values this becomes pretty easy. So let's make a list that has some duplicate values in it B, C, C, or let's make that a C. It doesn't really matter. myList is equal to list. Set myList and there we go. We've de-duplicated myList. And this highlights another important property of sets. The order doesn't matter. Sets aren't ordered lists, they're more like big bags of elements all jumbled up. Keep that in mind when you use this trick to de-duplicate lists because sets are randomized. The order of elements in your list may not be the same coming back out. Because of this, you can't fetch elements by their index like you can with a set. So if we take mySet and try to get the first or the element at the zero index, it won't let us do that. The set object is not subscriptable so you can't use the slicing syntax with it. In Python, an object is subscriptable. If it contains elements that can be accessed by an index, it contains ordered accessible subelements. Let's do something silly like number equals one and then we do the zeroth element of of one, right? We get that same error. The int object is not subscriptable. One doesn't contain any subelements and in a set it's not subscriptable because it doesn't make any sense to get the first element in sort of a big random pile. However, you can add elements to a set. So let's take mySet and then add D. And let's just take a look at mySet. There we go. Notice that this is different than the append function in the list, which specifically appends the element to the end of the list. Set uses the add function, which is kind of like tossing the element onto the pile. However, like with lists, you can use the membership operator to return boolean values. So if we say 'a' in mySet, it returns true because A is there. We say 'z' is in mySet, we get false. Also, like with lists, you can use the length function. So mySet should have four elements in it, perfect. And somewhat confusingly, set does have a pop function. You pop an element off the set. Of course, unlike with lists you're not going to get an element from the end of the set per se, but it'll grab an element for you and return it while removing the element from the set. So we can write something like while length of mySet mySet.pop, and let's make sure we print that out so that we can see the results. And it's just happening to return these in order here, but keep in mind that order is not guaranteed. If you want to remove a specific element, you can do that. So mySet.discard{'a'} Now notice that it didn't throw an error even though mySet is empty. It's just an empty set right now. Let's reset this again and then print out mySet. You can see it only contains the the elements B and C. Now tuples, let's move on to those. As a quick review, they're very much like lists. They have an order except they're declared with parentheses. So myTuple is 'a' 'b' need commas in there and 'c'. So while tuples pulls are ordered and subscriptable, the trick here is that you cannot modify them. So we can get tuple, get the first element of the tuple. The order does matter here, but if I try to do myTuple element at the index zero equals D, we're going to get an error there. Tuple does not support item assignment. Of course I can't show you any nifty functions for adding, removing, or popping elements off of tuples, because you cannot do that. You can't modify a tuple. Okay, why would we want to use tuples? Like I mentioned before, they are more efficient than list because Python doesn't have to worry about growing or changing them. It can allocate exactly as much space as it needs to in memory to really store these tuples compactly. But let's say you don't care about that. Another benefit is that they are often just kind of used by default. Let's write a python function that returns multiple values separated by commas. As it turns out, you can do the nifty trick in Python. returnsMultipleValues, return 1, 2, 3. What exactly is this function returning when we separated them by commas with this? Let's call it, and then put whatever it's returning into the type function. It returns a tuple. Okay? Now of course we can also do this, still the same type but it turns out that Python doesn't really require parentheses around tuples at all. So you could say myTuple is 1, 2, 3 and that's perfectly fine. myTuple, it's a tuple. Although for situations like this, if I saw this in code in the real world, this would look really odd to me. I would ask that the programmer please put parenthesis around that. But just so you know, you don't have to. And the really cool thing is that in the case where you're returning multiple values from functions this syntax without the parenthesis is actually preferred. So in this case, we prefer the syntax without the parenthesis but we do prefer that you add parentheses when you're just declaring a random Tuple. And back to this function here. If you want to set variables set many variables in a row, you can do this. So let's say A, B, C is equal to returnsMultipleValues, print A, print B, print C and unsurprisingly we have all these values set. This is called unpacking values, which we'll see again a little bit later in this chapter. So while tuples are sometimes stubbornly unchangeable, I think you'll find as you continue your Python journey that their convenience and elegance more than compensates in many situations.

Contents