Python Tutorial for Beginners 4: Lists, Tuples, and Sets — Transcript
Full transcript
- 0:00hey there how's it going everybody in
- 0:01this video we'll be learning about lists
- 0:03tupal and sets in Python now lists and
- 0:06tupal allow us to work with sequential
- 0:08data and sets are unordered collections
- 0:10of values with no duplicates and we'll
- 0:13look at all of these to see exactly what
- 0:15that means so first let's look at list
- 0:17and we're going to spend a majority of
- 0:19the time on list just because it has a
- 0:21lot more functionality than the other
- 0:22data types so just like the name implies
- 0:25it allows us to work with a list of
- 0:27values so for example let's say that I
- 0:29wanted a list of courses so I could
- 0:31create a variable here called courses
- 0:34and to create a list we're going to use
- 0:36these square brackets and Within These
- 0:39brackets we put each value that we want
- 0:42separated by a comma so for example
- 0:44let's say that we wanted a list of
- 0:46courses so I'll say history and math and
- 0:53physics and also lastly we'll put in
- 0:57compy so now if we print out this list
- 1:02so I'll print out courses and run that
- 1:05then we can see that it prints out our
- 1:06entire list now if we wanted to see how
- 1:09many values are in our list then we can
- 1:11get this by using the Len function which
- 1:14stands for length so if instead we print
- 1:16out the length using Len of courses and
- 1:20run that then you can see that it says
- 1:22that we have four values in our list and
- 1:24we can access each of these values
- 1:26individually so first let's go back to
- 1:28just printing out our list and running
- 1:32that so now to access the values in our
- 1:34list we can use square brackets after
- 1:37our list and pass in the location of the
- 1:40value that we want so we can put in
- 1:42square brackets and this location is
- 1:44called an index and it starts at zero so
- 1:47to access the first value of our list we
- 1:50can access uh the location at index0 if
- 1:54I print that then we can see that we get
- 1:56history now since the length of our list
- 1:58is four that means with the first value
- 2:01starting at index zero then the last
- 2:03value will be at index three so it's our
- 2:06total length minus one so let's say I
- 2:09wanted to grab the last value for my
- 2:11list which is compy I could count these
- 2:14indexes out and see which one should be
- 2:16able to access it so 0 1 2 and three the
- 2:21third index so if I print out courses
- 2:24three and access that index of three and
- 2:27run that then we can see that we printed
- 2:28out compy now we can actually use
- 2:31negative indexes too and negative
- 2:33indexes will just start from the end of
- 2:35the list so since zero is the first item
- 2:38of our list I can also get the last item
- 2:40of our list using a negative one so if
- 2:43we print out courses uh index of -1 and
- 2:47run that then we can see that we got
- 2:49compi again which is the last item it's
- 2:52more convenient a lot of the time using
- 2:55a negative one to get the last item
- 2:57because we don't have to worry about
- 2:59what the link length of the list is so
- 3:01for example if my list grew by 10 items
- 3:04then the third index would no longer be
- 3:06the last item but the negative -1 will
- 3:09always be the last item and also if you
- 3:11accidentally try to access an index that
- 3:13doesn't exist then you'll get an index
- 3:15error so in this example if we tried to
- 3:18access an item at index 4 and run that
- 3:21since there is no index 4 you can see
- 3:23that we get this list index out of range
- 3:26now instead of only grabbing one value
- 3:28we can also access a range of values so
- 3:31if I wanted to grab the first two values
- 3:33from this list then I could say that I
- 3:36want to access starting at index zero
- 3:39and go to but not including index two so
- 3:43this first index is our starting point
- 3:45and the second index which is separated
- 3:47by this colon is the stopping point now
- 3:50one thing that's a little confusing is
- 3:51that the first index is inclusive but
- 3:54the second index is not and there's good
- 3:56reasons for that but it's easy to forget
- 3:58so what we're saying here is that we
- 4:00want all of the values between the
- 4:04beginning and up to but not including
- 4:06the second index so if we run this then
- 4:10we can see that it printed out history
- 4:11and math because it printed out zero and
- 4:15one all the way up to two but not
- 4:17including two which is physics now since
- 4:19our first index here is just the start
- 4:21of the string we can actually just leave
- 4:23that off and it'll assume that we want
- 4:24to start at the beginning so if we said
- 4:27print the courses with nothing there
- 4:30colon 2 and run this then you can see
- 4:33that we get the same result because it
- 4:35just assumed that we wanted to start at
- 4:37the beginning now if we just wanted to
- 4:39grab physics and compi from this list
- 4:42then we can say that we want to start at
- 4:44index 2 and then put in our colon and
- 4:47now just like our starting index if we
- 4:49don't put anything here then it's going
- 4:52to assume that we want to go all the way
- 4:53to the end of the list so if I run this
- 4:56then you can see that it printed out
- 4:57physics and compi because it started at
- 5:00our second index which is physics and
- 5:02just went all the way to the end now
- 5:03what we're doing here is called slicing
- 5:05and if you'd like to learn more about
- 5:07slicing in depth then you can watch my
- 5:09detailed video on that which shows you
- 5:11how you can skip values go in reverse
- 5:13and things like that and I'll leave a
- 5:15link to that video in the description
- 5:16section below okay so let's look at some
- 5:19list methods that we have available to
- 5:21us that allow us to modify our list so
- 5:23first let's say that we wanted to add an
- 5:25item to our list there are a couple of
- 5:27ways we can do this so first if we just
- 5:30wanted to add an item to the end of our
- 5:32list then we could use the append method
- 5:35so let's say that we want to add art to
- 5:37our courses so we can just say
- 5:41courses. append and we want to append
- 5:44art so now if I remove this slicing and
- 5:47just print out our course's list and run
- 5:50that then you can see the art was
- 5:52appended here to the end of the list now
- 5:54if we wanted to instead add art to a
- 5:56specific location in our list then we
- 5:59could instead use the insert method now
- 6:01insert takes two arguments first it
- 6:03takes the index where you want the to
- 6:05insert the value and then the value
- 6:07itself so if I wanted to insert art to
- 6:10the beginning of our list then we could
- 6:13say
- 6:14courses. insert and now the first
- 6:17argument is the location so let's just
- 6:19say location zero which is the beginning
- 6:21and then the value that we want to
- 6:23insert which is Art so if we run this
- 6:26now we can see that art was inserted at
- 6:28position zero
- 6:30now that only inserted the value it
- 6:31didn't overwrite anything so you can see
- 6:33that all the other courses are still
- 6:35here but they just got shifted now
- 6:37another way of adding values to our list
- 6:40is using the extend method now sometimes
- 6:42this confuses people so let's look at
- 6:44what this does so we want to use extend
- 6:47when we have multiple values that we
- 6:49want to add to our list so for example
- 6:52let's say that we have another list here
- 6:54called courses 2 and we'll set this
- 6:57equal to another list uh with art and
- 7:01education in this list and we want to
- 7:04add these values to our original courses
- 7:07list so first let's see what happens if
- 7:10we use this insert method so instead of
- 7:13inserting art to the beginning of our
- 7:15list let's instead insert these courses
- 7:18to the beginning of our list so let's go
- 7:20ahead and run this so we can see that at
- 7:22the beginning of our list that it
- 7:25actually added the entire list of
- 7:27courses to and not each each individual
- 7:30value so we can actually have a list
- 7:33within a list like we have here so if we
- 7:36were to print the first value of our
- 7:38courses list so I'll print out the index
- 7:41of zero and run that then we can see
- 7:43that now the first value is actually
- 7:46this list itself but this isn't really
- 7:48what we wanted we wanted to add all of
- 7:50those values from our second list to our
- 7:53original list now that is why we use the
- 7:55extend method so let's go ahead and set
- 7:58this back to the way was to just print
- 8:00the courses now instead of inserting
- 8:02here we'll instead use extend and it
- 8:05only takes one argument which is the
- 8:08iterable so we will extend courses with
- 8:12courses to so now if we run this then we
- 8:15can see that when we did courses. extend
- 8:18with courses 2 that it added the values
- 8:21from our second list uh here to our
- 8:24original so a lot of people get that
- 8:26mixed up with append and extend so again
- 8:30uh if you were to append it just like
- 8:31with insert then it's just going to
- 8:33append the list itself on there instead
- 8:37of the each individual item so if we use
- 8:40extend then now we can see that each
- 8:42individual item is extended onto that
- 8:45list okay so now let's look at how we
- 8:48can remove some items or remove some
- 8:51values from our list now one way to
- 8:53remove values is to just use the remove
- 8:56method so if we were to say courses do
- 9:00remove then let's say that we wanted to
- 9:02remove math so if we save that and run
- 9:05it then we can see that math was removed
- 9:08from the courses list now there's also a
- 9:10way of removing values with this pop
- 9:13method so if we say courses. pop Now by
- 9:17default this will remove the last value
- 9:19of our list now this is useful if we
- 9:22want to use our list like a stack or a
- 9:25CU so if we run this like it is then we
- 9:28can see that comp I was popped of off of
- 9:31our list and now we just have these
- 9:33three courses now one other thing about
- 9:35pop is that it Returns the value that it
- 9:38removed so we can actually set a
- 9:40variable uh and grab that returned value
- 9:43so if I set a variable here and say
- 9:45popped equals courses. poop and then I
- 9:49was to print this above our courses here
- 9:52and run that then we can see that it
- 9:55grabbed that compi value that was popped
- 9:57off of the list so if you had a or a
- 9:59queue then you could go through and just
- 10:02keep popping off values until your list
- 10:04is empty okay so now let's look at how
- 10:07we can sort our list in a couple of
- 10:09different ways here so first of all
- 10:11let's say that we just wanted to reverse
- 10:13our list as it currently is now this is
- 10:15pretty easy so we can just use the
- 10:17reverse method so I I can say courses.
- 10:20reverse and if we run this then we can
- 10:23see that now it prints out our uh
- 10:26courses but in Reverse so the last item
- 10:29all the way up until the first item now
- 10:31instead of reversing our list what if we
- 10:33wanted to sort our list now sorting is
- 10:36just as easy we can just use the sort
- 10:39method so I'll save that and run it and
- 10:42you can see that now our list is sorted
- 10:44in an alphabetical order so compi
- 10:47history math physics now if our list
- 10:49contained numbers then it would sort
- 10:51those in ascending order so let me call
- 10:54uh create another list here of numbers
- 10:57I'll just call that nums I'll set this
- 10:59equal to some random values so I'll say
- 11:011 5 uh 4 and three and we'll save that
- 11:05now below courses we will just say nums
- 11:08do sort and we will also print these out
- 11:11so I'll grab that variable print those
- 11:14out and run that so now we can see that
- 11:17our strings were sorted alphabetically
- 11:19and our numbers were sorted in ascending
- 11:21order now what if we wanted our values
- 11:24sorted in descending order now one way
- 11:26you might think to do this is just to
- 11:28use our reverse me method on the list
- 11:30after they're sorted and that would work
- 11:32but there's an easier way to do this uh
- 11:34instead we can just pass an argument to
- 11:37our sort method called reverse so if I
- 11:39come up here to the sort method and I
- 11:41pass reverse is equal to true and let me
- 11:45also grab this for our num sort and if I
- 11:49run this you can see that now these
- 11:51sorted are in descending order they're
- 11:54sorted in reverse order now one thing to
- 11:56notice here is that we don't need to
- 11:59reset our variables when we call most of
- 12:02these methods it's just altering the
- 12:05list in place but there's also a way
- 12:07that we can get a sorted version of our
- 12:09list without altering the original list
- 12:12so what if we wanted a sorted version of
- 12:15our courses list without uh altering the
- 12:19original so to do this we can use the
- 12:21sorted function so instead of calling
- 12:24this sort method on our list we'll
- 12:26instead use this sorted function and
- 12:29I'll pass courses in to sorted there now
- 12:32if we run this as it is then we can see
- 12:35that our list is not sorted it's exactly
- 12:37how uh we described it up here now
- 12:40that's because the sorted function
- 12:42doesn't sort the list in place it
- 12:45returns a sorted version of the list so
- 12:47to get that sorted list we have to make
- 12:50a new variable and set it to the return
- 12:52value of the sorted function so I could
- 12:55just call this sorted courses is equal
- 12:58to
- 12:59the sorted version of that course's list
- 13:03and now if we copy that and print that
- 13:05out so now we can see that this sorted
- 13:07courses is equal to the sorted version
- 13:09of that list so that's really useful
- 13:11because a lot of the times you won't
- 13:13want to alter your original list in any
- 13:16way um so using this sorted function is
- 13:18a nice way to get a sorted version of
- 13:20that list without altering the original
- 13:23and other than this sorted function
- 13:24there are a couple more useful built-ins
- 13:27uh that we can use with these SE quences
- 13:29so let's look at a few of these so we'll
- 13:31look at min max and sum so it's probably
- 13:35pretty obvious what these will do but if
- 13:37I wanted the minimum value of our
- 13:40numbers list here then let me comment
- 13:43out where we're printing out nums so a
- 13:45built-in function that we can use is
- 13:47just men and call men on our sequence of
- 13:50numbers and if I run that then you can
- 13:52see that it return returns one as the
- 13:54minimum number of that list and if we
- 13:56wanted the max value of that list then
- 13:58we could use Max and if I run that you
- 14:01can see it returns five um now if we
- 14:04wanted to print the sum of that entire
- 14:06sequence that I can just say sum of
- 14:09numbers if I print that then it gets 15
- 14:12because 1 + 5 + 2+ 4 + 3 is equal to 15
- 14:16okay so lastly let's see how we can find
- 14:20some values here within our list so let
- 14:22me go ahead and clean up here a little
- 14:25bit and uncomment out that so if we
- 14:28wanted to find the index of a certain
- 14:30value then we can use the index method
- 14:32for this so if I wanted to find the
- 14:34index of compi in our list then we could
- 14:38just print out
- 14:41courses. index and then search for compi
- 14:46now if we run this then we can see that
- 14:48we got three and three is the index
- 14:51where it found that compai value now if
- 14:53we try to find the index of a value that
- 14:56doesn't exist in the list then we'll get
- 14:58a value value errror so if we search
- 15:01that original list of courses for art
- 15:03and run that then we can see that we got
- 15:05a value error and it says art is not in
- 15:08the list now if we just wanted to Simply
- 15:11check if the value was in our list and
- 15:13simply get back a true or false result
- 15:16then for this we can use the in operator
- 15:19so if I wanted to check if art is in our
- 15:21list then I could say art in courses so
- 15:26if I run that and we can see that we got
- 15:28false but if instead we said math in
- 15:32courses and ran that then that returned
- 15:34true now this is going to be especially
- 15:37useful once we go over the topic of
- 15:39conditionals and if else statements and
- 15:41we're going to go over those in a couple
- 15:42of videos and we can also use this to
- 15:45Loop through values of our list by using
- 15:48a for Loop so if I was to say four item
- 15:53in courses and then scoot that over then
- 15:57we will just print out
- 15:59that item now this is the first time in
- 16:03our series that we've indented a block
- 16:05of code basically what we're saying here
- 16:07is that we want to create a loop We're
- 16:10looping through each value of our list
- 16:12and each Loop through this item variable
- 16:15will be equal to the next item in the
- 16:18list so that's why this line is indented
- 16:21because it tells us that this code is
- 16:23executed from within our for Loop so if
- 16:26we run this then we can see that it
- 16:28prints out each item of our list now the
- 16:31reason it prints them all out on a new
- 16:33line is because by default the print
- 16:35statement goes to a new line each time
- 16:37it's executed now this item variable is
- 16:40just a variable it's not a keyword or
- 16:41anything we can name this anything we
- 16:43want so instead if we wanted to you know
- 16:46call this course for course and courses
- 16:49and print that out then you can see that
- 16:50we get the same result so we can access
- 16:52each value as We're looping through but
- 16:55sometimes it might be useful to also
- 16:57have the index of what value were on Now
- 17:00to do this in Python we can access the
- 17:02index and the value by using the
- 17:05enumerate function so I will say
- 17:08enumerate and I will wrap courses within
- 17:11that enumerate function and this
- 17:13enumerate function returns two values it
- 17:15Returns the index that we're on and the
- 17:17value so instead of just getting the
- 17:19course here we're also going to need to
- 17:21get the index so I'll call that index so
- 17:24for index course in enumerate courses
- 17:28and now if we print out that index and
- 17:30the course and run that then you can see
- 17:33that we had access to each index and
- 17:36value as We're looping through our list
- 17:38and if we don't want to start at zero
- 17:41then we can pass in a start value to our
- 17:43numerate function so if instead we
- 17:45wanted to start at one then I can pass
- 17:50in the second argument say start is
- 17:52equal to one now if I run this then you
- 17:54can see that now our starting value is
- 17:57one okay so there is one one more thing
- 17:59that I want to go over before we move on
- 18:01to tupal and sets so it's pretty common
- 18:04that we'll want to turn our list into
- 18:06Strings separated by a certain value now
- 18:09to do this we're actually going to use a
- 18:11string method called join and we're
- 18:13going to pass in our list as the
- 18:15argument so for example let's say that
- 18:17we wanted to turn our list of courses
- 18:20into a string of comma separated values
- 18:22so we can say course string and that
- 18:25will hold the string version of our
- 18:27courses and now we'll type the string
- 18:29that we want to separate each item of
- 18:31our list so I want these to be comma
- 18:33separated so I'll put in a string of a
- 18:37comma and a space and then I'm going to
- 18:39use the join method to join the values
- 18:41of the list using this string so I'll
- 18:44say dojin and I want to join my courses
- 18:48on that string so now if I come down
- 18:50here and print that string version of
- 18:54our courses and run that then we can see
- 18:56that we get our values comma separated
- 18:58and if we want to change this then we
- 19:00can just change our string within uh
- 19:03that we're joining on so if we wanted to
- 19:05hyphenate these then I could just change
- 19:07this to space hyphen space and run that
- 19:10and you can see that when we ran that
- 19:12now we're separating these by The Hyphen
- 19:14so this is just one long string with all
- 19:17of our list values joined together now
- 19:19we can also do the reverse of this and
- 19:21turn a string back into a list so we can
- 19:24do this by splitting our string on a
- 19:26certain value so if I split the string
- 19:28that that we just created so let me
- 19:30create a new list here and I'll set this
- 19:33equal to the string version of our list
- 19:36and I'll do a DOT split and I'll split
- 19:39it on a space hyphen space so if we look
- 19:43at the string version that we have down
- 19:45here this is just one whole string what
- 19:47that's saying is it's saying hey split
- 19:49up all of these values on this space
- 19:51comma space and make a list out of all
- 19:53those values that you get so now if we
- 19:56print out our new list so I'll print
- 19:59that new list and run that and you can
- 20:02see that now we're back to the original
- 20:04list okay so I know that that was a lot
- 20:07to take in because there's so much that
- 20:08we can do with list but now let's move
- 20:11on to tupal and sets um now that we're
- 20:14familiar with list then these will
- 20:16probably go pretty quickly okay so tups
- 20:18are very similar to list but with one
- 20:21major difference so we can't modify
- 20:23tupal now in programming this is called
- 20:26mutable and immutable so list are
- 20:29mutable and tuples are not they are
- 20:31immutable so let's look at what this
- 20:33means so I'm going to grab a quick
- 20:35snippet of code here from my Snippets
- 20:38file uh just to compare these so I'm
- 20:40just going to grab from here all the way
- 20:43down to here and just go ahead and paste
- 20:47those in so first we have a regular list
- 20:50here that we've already looked at and
- 20:52we're going to look at one issue that
- 20:54you might run into with mutable objects
- 20:56so what I'm doing here is I'm creating
- 20:59our list of courses and I'm calling this
- 21:02list one and then we're creating another
- 21:04variable here called list two and we're
- 21:07setting that equal to list one and then
- 21:09we're printing out both of these so if I
- 21:12run this then we can see that both list
- 21:14one and list two have the same values so
- 21:17now let me uncomment out this code where
- 21:20we're changing a value in the first
- 21:23index of list one and then reprinting
- 21:26our values again so now if I run this
- 21:29then we can see here that all we did was
- 21:31change the value at the first index of
- 21:34list one but by changing list one's
- 21:37first value it also changed list two and
- 21:40the reason for that is because they're
- 21:42both the same mutable object now if you
- 21:45need to modify your list then this
- 21:47mutability is what you want but if you
- 21:50want a list of values that you no aren't
- 21:53going to change then we can use a tuple
- 21:55so let's look at a tupal example so I'm
- 21:58going to Common out our mutable example
- 22:00here and then uncommon out our Tuple
- 22:03example so a tupal looks almost exactly
- 22:06like a list but instead of the square
- 22:09brackets we're using instead using these
- 22:11parentheses and after we create our
- 22:13tupal we're doing the exact same thing
- 22:15we're calling a uh creating a variable
- 22:18Tuple one that is a tuple and then we're
- 22:21setting this Tuple two equal to Tuple
- 22:24one and for now I'll just comment out
- 22:26where we're changing that first value
- 22:28and I'll run that and you can see that
- 22:30both of these tupal are equal to the
- 22:33same list of values but now if we try to
- 22:36change that value at the first index of
- 22:39Tuple one just like we did with our list
- 22:42if we run that then we can see here that
- 22:44we get an error and it says type error
- 22:46Tuple does not support item assignment
- 22:49and that's because it's immutable um so
- 22:52now since a tupal is immutable it
- 22:55doesn't have nearly as many methods as a
- 22:58list because a lot of those list methods
- 23:00that we looked at involved mutating the
- 23:03values so we can't append we can't
- 23:05remove anything or anything like that
- 23:08but other than that they behave pretty
- 23:09much the same we can Loop through tupal
- 23:12we can access values and all the other
- 23:14things that we've already seen except
- 23:17for what mutates the list so that's
- 23:19basically the difference between list
- 23:21and tupal um if you need something that
- 23:23you can modify then use a list but if
- 23:26you just want something that you can
- 23:27Loop through and access then you might
- 23:30want to think about a tupal so that you
- 23:32don't run into the issues that we just
- 23:34saw okay and lastly let's look at sets
- 23:37now sets are values that are unordered
- 23:40and also have no duplicates so let me
- 23:43grab a sample from the Snippets file
- 23:45here um that we can see so I'm just
- 23:48going to go ahead and grab this small
- 23:50snippet and paste that in so we can see
- 23:52that this looks similar to lists and
- 23:54tupes but instead of the brackets or
- 23:57parentheses we're we're instead using
- 23:59these curly braces now if I run this
- 24:02code to print this set out then we can
- 24:04see that it prints out the values but if
- 24:06you notice here these aren't in the
- 24:07order that we added them so we had
- 24:09history first here and it has history
- 24:12last down here and if I run this a
- 24:14couple more times then we'll see that
- 24:16you know this order can change with each
- 24:19execution now the reason for this is
- 24:21because unlike our lists or tupal sets
- 24:24don't really care about order because
- 24:26some of the main uses for a set is
- 24:29either to test whether a value is part
- 24:31of a set and also it's used a lot to
- 24:33remove duplicate values because sets
- 24:35throw away duplicates so first let's
- 24:38look at how to get rid of duplicates so
- 24:40if I add another Math course here to the
- 24:43end of our set and now if I rerun this
- 24:47then we can see that we still only have
- 24:49four courses it got rid of that second
- 24:51Math course and just left us with the
- 24:54one now another thing I mentioned was
- 24:56that it's used to test whether a value
- 24:58is part of a set now this is called a
- 25:01membership test so sets do this a lot
- 25:03more efficiently than list and tupal so
- 25:06what I mean by this is that within our
- 25:08print statement if I was to say um math
- 25:13in CS courses and I run that then when
- 25:17you can see that it prints out true now
- 25:19we could do that with list and tups also
- 25:22but sets are optimized for this okay and
- 25:25lastly something else really useful that
- 25:27sets can quickly do is determine what
- 25:30values they either share or don't share
- 25:32with other sets so for example let me
- 25:35create another set called Art courses
- 25:38and I'll create this pretty similar to
- 25:40CS courses here um but instead of
- 25:44physics and compi I'll instead say Art
- 25:47and Design and with both of these I'm
- 25:49going to go ahead and take out that
- 25:51extra uh math on the end just so it's a
- 25:54little bit more clear and instead of
- 25:55this being CS courses I'm going to call
- 25:57this set art courses so now we have two
- 26:00different sets here and some of them
- 26:02have different courses and some of the
- 26:04courses are the same so let's say that I
- 26:06wanted to see what courses these sets
- 26:09had in common so to do this we could use
- 26:11the intersection method so we can say CS
- 26:16courses.
- 26:19intersection and we will pass in the art
- 26:22courses into that so if we run that then
- 26:25we can see that it shows us that the
- 26:27history and math coures are in both of
- 26:29those sets and if I wanted to see what
- 26:31courses are in the Cs courses but not
- 26:35the art courses then I could use the
- 26:37difference method so instead of
- 26:39intersection I will instead say
- 26:41difference so if I run that then now we
- 26:43can see that it shows that the uh
- 26:46physics and compsite courses weren't in
- 26:48the art courses now if I wanted to
- 26:51combine both of these sets and print all
- 26:54of the courses offered then I could use
- 26:56the union method so so I'll say CS
- 26:59courses. Union and we want to Union with
- 27:02the art courses so if I run that then we
- 27:05can see that now we get all of the
- 27:06courses printed out from both sets so
- 27:09sets can definitely be useful for these
- 27:11kinds of use cases and for these
- 27:13particular problems they're much more
- 27:15performant than lists or tupal okay so
- 27:18we are basically finished up with this
- 27:19video but let me show you one last thing
- 27:21before we close this out and that's
- 27:23going to be how to create empty list
- 27:25tupes and sets so I have these over here
- 27:28and my Snippets so let me grab these
- 27:31real quick and copy and paste these over
- 27:35here and bring this down a line so I
- 27:37wanted to show this because there's a
- 27:39small gotcha when it comes to creating
- 27:41an empty set so to create an empty list
- 27:45we can either set it equal to empty
- 27:47square brackets or we can use this
- 27:49built-in list class and to create an
- 27:52empty tupal we can use these empty
- 27:54parentheses or this built-in tupal class
- 27:57now to create an empty set we actually
- 27:59can't use these empty curly braces um so
- 28:03this line right here is wrong that's not
- 28:05an empty set this is actually going to
- 28:08create an empty dictionary so to create
- 28:10an empty set the way to properly do this
- 28:13is to use the built-in set class with no
- 28:16values and speaking of dictionaries
- 28:18that's actually what we're going to
- 28:19cover in our next video but I hope
- 28:22everyone feels comfortable now with
- 28:23working with lists and tupal and sets
- 28:26but if anyone does have any questions
- 28:27about about what we covered in this
- 28:29video then feel free to ask in the
- 28:30comment section below and I'll do my
- 28:31best to answer those now if you enjoy
- 28:33these tutorials and would like to
- 28:34support them then there are several ways
- 28:36you can do that the easiest way is to
- 28:38Simply like the video and give it a
- 28:39thumbs up and also it's a huge help to
- 28:41share these videos with anyone who you
- 28:42think would find them useful and if you
- 28:44have the means you can contribute
- 28:45through patreon and there's a link to
- 28:47that page in the description section
- 28:48below be sure to subscribe for future
- 28:50videos and thank you all for watching
About this transcript
This page contains the full transcript of Python Tutorial for Beginners 4: Lists, Tuples, and Sets by Corey Schafer, generated from the public captions YouTube serves with the video. The transcript has 5,473 words across 692 segments, with the original timestamps preserved so you can click any line to jump to that moment in the embedded player.
What you can do with it
Use the transcript to take notes, quote the speaker, build a study guide, generate a summary with ChatGPT or Claude via the YouTube Summary tool, or export it as a timed subtitle file with YouTube to SRT. You can also re-open it in the transcriber to translate the transcript into 100+ languages.
Free YouTube transcript tool
YouTube2Text is a free YouTube transcript generator — no signup, no daily limit. Paste any YouTube link and get the full transcript instantly, with timestamps, click-to-jump, translation to 100+ languages, AI prompts for ChatGPT, Claude, and Gemini, and exports to TXT, SRT, VTT, or Markdown.