Coding

Tuples & Tuple Unpacking in Python

  • 00:00:02 welcome to this video this video is
  • 00:00:04 about tuples and tuple unpacking or
  • 00:00:07 multiple assignment tuples are a later
  • 00:00:10 type in Python quite comparable to lists
  • 00:00:13 with one important difference and tuple
  • 00:00:15 unpacking or multiple assignment is a
  • 00:00:19 concept that allows us to conveniently
  • 00:00:22 assign different values to different
  • 00:00:24 variables so let's see how this works in
  • 00:00:27 this video now
  • 00:00:34 tuples are immutable lists in the end
  • 00:00:37 and for that I will now create a tuple
  • 00:00:39 and the list to show you the differences
  • 00:00:42 and similarities between these two data
  • 00:00:45 types let's start with a user names list
  • 00:00:49 maybe and let's say this list should
  • 00:00:51 contain one list item surrounded by d
  • 00:00:54 square brackets right here and this
  • 00:00:56 could be my name manual let's create the
  • 00:01:00 same thing with a tuple now for that
  • 00:01:03 we'll again define a variable name in
  • 00:01:06 this case user names tuple and now I
  • 00:01:08 will add the name like that this would
  • 00:01:12 be almost a tuple there is one important
  • 00:01:15 thing missing I'll come back to that in
  • 00:01:16 a few seconds you could also add
  • 00:01:18 parentheses right here like this and
  • 00:01:20 write that this is optional though I
  • 00:01:23 would recommend to do so because it
  • 00:01:25 makes the code a bit more explicit here
  • 00:01:27 with that we can now actually print our
  • 00:01:32 user names list and also our user names
  • 00:01:35 Cupid right here or let's maybe print
  • 00:01:38 the type of these two variables here and
  • 00:01:46 if we now run the code you can see two
  • 00:01:50 interesting things the first part right
  • 00:01:54 here is a list this worked fine because
  • 00:01:56 the square brackets indicates that we
  • 00:01:59 have a list right here but the second
  • 00:02:01 variable is still a string so our
  • 00:02:03 parentheses right here this optional
  • 00:02:05 parenthesis we can use wasn't enough to
  • 00:02:08 tell – that we have a tuple right here
  • 00:02:10 it will become a tuple though if we add
  • 00:02:12 a comma right here after our first item
  • 00:02:15 though this syntax looks a bit strange
  • 00:02:17 at first you can see that we now created
  • 00:02:20 a list and a tuple
  • 00:02:21 so that's the first coughing tuples can
  • 00:02:23 be created with or without parentheses
  • 00:02:25 but if you have a tuple containing one
  • 00:02:27 single item only make sure to add the
  • 00:02:30 comma after this first item to tell
  • 00:02:32 python that this should be a tuple for
  • 00:02:34 the list this is not necessary because
  • 00:02:36 these square brackets already indicate
  • 00:02:38 that this should be a list let's add
  • 00:02:40 some more names now max James and Harry
  • 00:02:46 like that
  • 00:02:47 and let's also add the same names right
  • 00:02:50 here or tubal if we now get rid of our
  • 00:02:55 type right here and right there and
  • 00:02:59 print this we can see both work kind of
  • 00:03:04 in the same way no big differences here
  • 00:03:06 we can also access lists and tuple items
  • 00:03:10 the same way by indexing so if we say we
  • 00:03:14 want to access the first list item right
  • 00:03:15 here and right there
  • 00:03:17 this works for both we selected manual
  • 00:03:20 involve the tuple and the list we can
  • 00:03:23 also use a concept name slicing to
  • 00:03:25 select a range of items inside a list or
  • 00:03:28 a tuple so we can say we want to start
  • 00:03:30 at the index of zero so manual then we
  • 00:03:33 add a colon and then we define the start
  • 00:03:36 value this would be the index of two
  • 00:03:38 right here so this one but important it
  • 00:03:40 starts right here at zero and then it
  • 00:03:43 ends with two but the stop value so two
  • 00:03:45 right here will be executed so we will
  • 00:03:47 basically print manual and next right
  • 00:03:50 here same thing as possible for the
  • 00:03:53 tuple if you run the code we print
  • 00:03:55 manual at max for both the list and the
  • 00:03:58 tuple so selecting list items on tuple
  • 00:04:01 items via indexing or slicing works fine
  • 00:04:04 but as tuples are immutable changing
  • 00:04:08 values stored in a tuple doesn't work as
  • 00:04:11 it does for lists for that let's maybe
  • 00:04:14 delete our slicing part right here and
  • 00:04:18 let me now change the user names list
  • 00:04:21 first item so max to Sam maybe let's
  • 00:04:27 edit right here and let's not do the
  • 00:04:29 same thing for our tuple right here if I
  • 00:04:34 run the code now you can see already
  • 00:04:36 that the IDE already tells me that we
  • 00:04:38 have a problem here so that the item
  • 00:04:41 assignment is not supported nevertheless
  • 00:04:43 let's run the code and as you can see up
  • 00:04:46 here for our list
  • 00:04:47 Sam was changed so Sam was added so we
  • 00:04:51 don't have max here anymore but we have
  • 00:04:53 Sam but for our tuple this doesn't work
  • 00:04:55 same thing is true for the methods
  • 00:04:58 available for lists and tuples if we
  • 00:05:00 delete that part
  • 00:05:01 now at the dot we can see we have a lot
  • 00:05:04 of different methods available for our
  • 00:05:05 list for example append to add a list
  • 00:05:08 item or pop to remove a list item if I
  • 00:05:11 select a pen right here and I'll say I
  • 00:05:14 want to add a Sam at the end now then I
  • 00:05:17 can do this perfectly with my list but
  • 00:05:19 if I would like to do the same thing
  • 00:05:20 with the tuple you can see we only have
  • 00:05:23 two methods available count and index I
  • 00:05:26 will show these methods in a few second
  • 00:05:28 let me just run the code again to show
  • 00:05:30 you that Sam was added successfully
  • 00:05:31 right here to our list for a tuple we
  • 00:05:34 didn't have that option now what about
  • 00:05:36 these two methods I was referring to you
  • 00:05:38 for that let's simply add the method
  • 00:05:41 right here and let's say we want to have
  • 00:05:43 a look at count first right here we have
  • 00:05:47 to add the parentheses right here of
  • 00:05:48 course and now we can count how many
  • 00:05:50 times a specific well value is stored in
  • 00:05:54 our tuple so for example if I add Manuel
  • 00:05:57 right here and if I print that you can
  • 00:06:00 see we get back one because manual is
  • 00:06:02 available or stored once right here in
  • 00:06:04 our tuple if I would add Manuel twice at
  • 00:06:07 the end and run the code once again we
  • 00:06:09 get back to because Manuel well of
  • 00:06:11 course two times right here in our tuple
  • 00:06:13 we don't need that though so we can
  • 00:06:15 actually delete it once again the second
  • 00:06:18 method we had was index index right here
  • 00:06:21 this one index basically shows us the
  • 00:06:24 index of a specific value so where it is
  • 00:06:26 stored in our tuple if you run the code
  • 00:06:28 you can see manually stored at the index
  • 00:06:31 of zero whereas James for example has
  • 00:06:36 the index of two so these are the two
  • 00:06:39 methods we have for our tuples and with
  • 00:06:41 that we saw already the core difference
  • 00:06:44 between a less than a tuple lists are
  • 00:06:45 mutable tuples are immutable now this
  • 00:06:49 brings us to the question why would I
  • 00:06:51 want to use such tuples and for that I
  • 00:06:53 will comment out this entire code up
  • 00:06:56 here and start a new example because to
  • 00:07:00 understand why Cubans can be very useful
  • 00:07:02 I want to introduce a second concept to
  • 00:07:06 you the so called tuple unpacking or
  • 00:07:08 multiple assignment tuple unpacking is
  • 00:07:12 not necessarily related to tuples only
  • 00:07:15 can also apply the same concept to lists
  • 00:07:17 for example I will use it for tuples
  • 00:07:19 though in this video here and tuple
  • 00:07:22 unpacking or multiple assignment simply
  • 00:07:25 allows you to assign multiple values to
  • 00:07:28 multiple variables very conveniently for
  • 00:07:31 that let me create another example maybe
  • 00:07:34 with the user data variable right here
  • 00:07:38 and this should be a tuple so it will
  • 00:07:40 create the parentheses and this should
  • 00:07:42 contain my name so the user name the age
  • 00:07:45 41 and maybe I'm the gender which in my
  • 00:07:48 case would be male like that this is a
  • 00:07:52 tuple we learned about that and now you
  • 00:07:55 can access these values in a tuple via
  • 00:07:58 indexing we sorted already we can send
  • 00:08:00 user data zero if we print that you can
  • 00:08:04 access manual right here so my name this
  • 00:08:07 is working fine but what if you want you
  • 00:08:09 assign of variable names to these values
  • 00:08:12 stored in the tuple so manual for
  • 00:08:14 example would be the name variable 31
  • 00:08:16 could be the age variable and mail could
  • 00:08:19 be the gender variable what you can do
  • 00:08:22 of course is you can say that um name
  • 00:08:24 could be user data 0 and if you print
  • 00:08:28 that name then you get it back and you
  • 00:08:30 could do the same thing for the
  • 00:08:32 remaining values and this is where
  • 00:08:34 multiple assignment comes into play
  • 00:08:36 because we cannot simply say that name
  • 00:08:40 age and gender are equal to user data
  • 00:08:45 and if I now print the name I get back
  • 00:08:48 the same result manual but what happened
  • 00:08:51 here
  • 00:08:51 we simply assigned name so the first
  • 00:08:54 variable to the first value stored
  • 00:08:56 inside our user data variable the second
  • 00:09:01 variable age was related or assigned to
  • 00:09:05 the second value stored inside our tuple
  • 00:09:08 and gender right here this variable was
  • 00:09:12 assigned to the third value stored
  • 00:09:14 inside our two blood so what we did
  • 00:09:16 right here is the so-called tuple
  • 00:09:18 packing right here so we basically added
  • 00:09:21 values to our tuple and what we did
  • 00:09:23 right here is the so called tuple
  • 00:09:25 unpacking we assigned a variable names
  • 00:09:28 to
  • 00:09:29 our values stored in the tuple tuple
  • 00:09:32 unpacking or multiple assignment is very
  • 00:09:34 explicit though this means if I would
  • 00:09:37 now add a fourth value here for example
  • 00:09:41 could be something like hobbies which
  • 00:09:43 could be cooking right lifting around
  • 00:09:45 the code we get an error so we have too
  • 00:09:47 many values to unpack right here in our
  • 00:09:49 tuple unpacking procedure same thing
  • 00:09:53 happens if I add too many variables
  • 00:09:54 let's say hobbies right here I wasn't
  • 00:09:59 gonna error we have not enough values to
  • 00:10:01 unpack because we expect four values we
  • 00:10:03 only get free so these three values here
  • 00:10:05 so you have to be careful to match the
  • 00:10:09 amount of values and the amount of
  • 00:10:10 variables right here there is one other
  • 00:10:13 approach though which makes sure that
  • 00:10:15 you well can have more values than
  • 00:10:18 variable names so if you for example add
  • 00:10:21 here again I'm cooking and I don't know
  • 00:10:26 maybe Germany sort of country we're
  • 00:10:28 living then you can simply add this
  • 00:10:30 asterisk right here and add a different
  • 00:10:34 variable so other for example right here
  • 00:10:37 if you do that now and print other right
  • 00:10:42 here then you can see that we don't get
  • 00:10:44 an error but that we assign the
  • 00:10:46 remaining values soldering values which
  • 00:10:48 are not covered in this first free
  • 00:10:50 variable so we have the first variable
  • 00:10:51 manual the second age deferred mail and
  • 00:10:54 the for phone with the asterisk right
  • 00:10:56 here simply includes all the other
  • 00:10:58 values we have at here so if I would add
  • 00:11:00 more values here this would also be
  • 00:11:02 printed and stored inside this variable
  • 00:11:05 we don't need that here so let's delete
  • 00:11:09 that quickly here and right there
  • 00:11:11 so this is tuple unpacking or multiple
  • 00:11:13 assignment we can't have other anymore
  • 00:11:16 but still we have these two concepts we
  • 00:11:18 have now the tuple with the immutable
  • 00:11:20 way of storing values and we have that
  • 00:11:22 tuple unpacking which allows us to
  • 00:11:24 assign multiple values to variables
  • 00:11:27 let's bring these concepts together now
  • 00:11:30 in a final example where we have a
  • 00:11:32 little function or a little user
  • 00:11:34 interface you could say in this
  • 00:11:36 interface I want the user to input its
  • 00:11:38 user email which should be an input
  • 00:11:42 function
  • 00:11:43 which says enter your email right here
  • 00:11:48 and we have a second example with the
  • 00:11:52 user password that maybe with a key
  • 00:11:59 right here this should now be stored in
  • 00:12:02 a function let's name it get user data
  • 00:12:06 no arguments needed make sure to do the
  • 00:12:08 indentation correctly here and now this
  • 00:12:11 function should return a tuple which
  • 00:12:14 includes the user input so we want to
  • 00:12:17 return two things user email and user
  • 00:12:24 password we don't have to add
  • 00:12:28 parentheses right here we create a tuple
  • 00:12:30 but we can add these to be a bit more
  • 00:12:33 explicit again what about you do know is
  • 00:12:36 I simply want to say that signup data is
  • 00:12:43 equal to our get user data function
  • 00:12:46 right here and now I want to print our
  • 00:12:48 signup data right there if we comment
  • 00:12:54 out this previous code right here and
  • 00:12:57 now we run the code you can see we can
  • 00:13:01 enter an email test at Tesco maybe
  • 00:13:03 password one two three and you can see
  • 00:13:06 we print these inputs inside our tuple
  • 00:13:10 and that's great already because with
  • 00:13:12 that you see one application area of a
  • 00:13:15 tuple because this would be a pretty
  • 00:13:17 common use case the user enters an email
  • 00:13:21 and a password to register on your
  • 00:13:23 website for example and then you can
  • 00:13:25 store these data right here with the
  • 00:13:27 tuple so it is immutable we cannot
  • 00:13:30 change the information stored inside the
  • 00:13:32 tuple anymore for example if I want to
  • 00:13:34 access the email so the first value
  • 00:13:37 stored in a tuple and if I want to
  • 00:13:39 change the email to test you at
  • 00:13:42 test comm right here ever run the code
  • 00:13:47 once again we can enter tests at test
  • 00:13:49 comm right one two three but here we get
  • 00:13:52 an error on our because we again cannot
  • 00:13:55 change the values stored in a tuple so
  • 00:13:57 if that you have a way to store the
  • 00:13:59 email and the password in an immutable
  • 00:14:01 way we can also add our tuple unpacking
  • 00:14:06 concept right here because as you saw I
  • 00:14:08 have to use the index right there to
  • 00:14:10 access the user email and in my case I
  • 00:14:12 know that the email is the first value
  • 00:14:14 stored in a tuple but how would I know
  • 00:14:15 that if I don't have this additional
  • 00:14:16 information so for that I can use to
  • 00:14:20 unpacking once again to say that email
  • 00:14:22 and password are the two values we
  • 00:14:25 stored inside our tuple so email and
  • 00:14:27 password is equal to sign up data and if
  • 00:14:30 I now print email for example like that
  • 00:14:34 I can refer to it explicitly and you can
  • 00:14:38 see if I enter test and test comm once
  • 00:14:41 again one two three we get back our
  • 00:14:44 email right here stored inside our tuple
  • 00:14:48 and that's it about tuples and tuple
  • 00:14:51 unpacking already as you can see it's
  • 00:14:53 quite easy to use actually the only
  • 00:14:56 thing we have to understand is that
  • 00:14:58 tuples are immutable lists are mutable
  • 00:15:00 and the tuple unpacking or multiple
  • 00:15:03 assignment doesn't mean anything more
  • 00:15:06 than assigning multiple variable names
  • 00:15:09 to multiple values so if you liked the
  • 00:15:11 video make sure to check out our other
  • 00:15:14 content on Academy on calm or here on
  • 00:15:16 the YouTube channel or subscribe to the
  • 00:15:18 channel if you like what we're doing and
  • 00:15:20 with that I hope to see you in one of
  • 00:15:22 the next videos too thanks for watching
  • 00:15:24 and bye