YouTube2Text

Python Tutorial for Beginners 4: Lists, Tuples, and Sets — Transcript

by Corey Schafer · 5,473 words · 692 segments · language en · Watch on YouTube

Full transcript

  1. 0:00hey there how's it going everybody in
  2. 0:01this video we'll be learning about lists
  3. 0:03tupal and sets in Python now lists and
  4. 0:06tupal allow us to work with sequential
  5. 0:08data and sets are unordered collections
  6. 0:10of values with no duplicates and we'll
  7. 0:13look at all of these to see exactly what
  8. 0:15that means so first let's look at list
  9. 0:17and we're going to spend a majority of
  10. 0:19the time on list just because it has a
  11. 0:21lot more functionality than the other
  12. 0:22data types so just like the name implies
  13. 0:25it allows us to work with a list of
  14. 0:27values so for example let's say that I
  15. 0:29wanted a list of courses so I could
  16. 0:31create a variable here called courses
  17. 0:34and to create a list we're going to use
  18. 0:36these square brackets and Within These
  19. 0:39brackets we put each value that we want
  20. 0:42separated by a comma so for example
  21. 0:44let's say that we wanted a list of
  22. 0:46courses so I'll say history and math and
  23. 0:53physics and also lastly we'll put in
  24. 0:57compy so now if we print out this list
  25. 1:02so I'll print out courses and run that
  26. 1:05then we can see that it prints out our
  27. 1:06entire list now if we wanted to see how
  28. 1:09many values are in our list then we can
  29. 1:11get this by using the Len function which
  30. 1:14stands for length so if instead we print
  31. 1:16out the length using Len of courses and
  32. 1:20run that then you can see that it says
  33. 1:22that we have four values in our list and
  34. 1:24we can access each of these values
  35. 1:26individually so first let's go back to
  36. 1:28just printing out our list and running
  37. 1:32that so now to access the values in our
  38. 1:34list we can use square brackets after
  39. 1:37our list and pass in the location of the
  40. 1:40value that we want so we can put in
  41. 1:42square brackets and this location is
  42. 1:44called an index and it starts at zero so
  43. 1:47to access the first value of our list we
  44. 1:50can access uh the location at index0 if
  45. 1:54I print that then we can see that we get
  46. 1:56history now since the length of our list
  47. 1:58is four that means with the first value
  48. 2:01starting at index zero then the last
  49. 2:03value will be at index three so it's our
  50. 2:06total length minus one so let's say I
  51. 2:09wanted to grab the last value for my
  52. 2:11list which is compy I could count these
  53. 2:14indexes out and see which one should be
  54. 2:16able to access it so 0 1 2 and three the
  55. 2:21third index so if I print out courses
  56. 2:24three and access that index of three and
  57. 2:27run that then we can see that we printed
  58. 2:28out compy now we can actually use
  59. 2:31negative indexes too and negative
  60. 2:33indexes will just start from the end of
  61. 2:35the list so since zero is the first item
  62. 2:38of our list I can also get the last item
  63. 2:40of our list using a negative one so if
  64. 2:43we print out courses uh index of -1 and
  65. 2:47run that then we can see that we got
  66. 2:49compi again which is the last item it's
  67. 2:52more convenient a lot of the time using
  68. 2:55a negative one to get the last item
  69. 2:57because we don't have to worry about
  70. 2:59what the link length of the list is so
  71. 3:01for example if my list grew by 10 items
  72. 3:04then the third index would no longer be
  73. 3:06the last item but the negative -1 will
  74. 3:09always be the last item and also if you
  75. 3:11accidentally try to access an index that
  76. 3:13doesn't exist then you'll get an index
  77. 3:15error so in this example if we tried to
  78. 3:18access an item at index 4 and run that
  79. 3:21since there is no index 4 you can see
  80. 3:23that we get this list index out of range
  81. 3:26now instead of only grabbing one value
  82. 3:28we can also access a range of values so
  83. 3:31if I wanted to grab the first two values
  84. 3:33from this list then I could say that I
  85. 3:36want to access starting at index zero
  86. 3:39and go to but not including index two so
  87. 3:43this first index is our starting point
  88. 3:45and the second index which is separated
  89. 3:47by this colon is the stopping point now
  90. 3:50one thing that's a little confusing is
  91. 3:51that the first index is inclusive but
  92. 3:54the second index is not and there's good
  93. 3:56reasons for that but it's easy to forget
  94. 3:58so what we're saying here is that we
  95. 4:00want all of the values between the
  96. 4:04beginning and up to but not including
  97. 4:06the second index so if we run this then
  98. 4:10we can see that it printed out history
  99. 4:11and math because it printed out zero and
  100. 4:15one all the way up to two but not
  101. 4:17including two which is physics now since
  102. 4:19our first index here is just the start
  103. 4:21of the string we can actually just leave
  104. 4:23that off and it'll assume that we want
  105. 4:24to start at the beginning so if we said
  106. 4:27print the courses with nothing there
  107. 4:30colon 2 and run this then you can see
  108. 4:33that we get the same result because it
  109. 4:35just assumed that we wanted to start at
  110. 4:37the beginning now if we just wanted to
  111. 4:39grab physics and compi from this list
  112. 4:42then we can say that we want to start at
  113. 4:44index 2 and then put in our colon and
  114. 4:47now just like our starting index if we
  115. 4:49don't put anything here then it's going
  116. 4:52to assume that we want to go all the way
  117. 4:53to the end of the list so if I run this
  118. 4:56then you can see that it printed out
  119. 4:57physics and compi because it started at
  120. 5:00our second index which is physics and
  121. 5:02just went all the way to the end now
  122. 5:03what we're doing here is called slicing
  123. 5:05and if you'd like to learn more about
  124. 5:07slicing in depth then you can watch my
  125. 5:09detailed video on that which shows you
  126. 5:11how you can skip values go in reverse
  127. 5:13and things like that and I'll leave a
  128. 5:15link to that video in the description
  129. 5:16section below okay so let's look at some
  130. 5:19list methods that we have available to
  131. 5:21us that allow us to modify our list so
  132. 5:23first let's say that we wanted to add an
  133. 5:25item to our list there are a couple of
  134. 5:27ways we can do this so first if we just
  135. 5:30wanted to add an item to the end of our
  136. 5:32list then we could use the append method
  137. 5:35so let's say that we want to add art to
  138. 5:37our courses so we can just say
  139. 5:41courses. append and we want to append
  140. 5:44art so now if I remove this slicing and
  141. 5:47just print out our course's list and run
  142. 5:50that then you can see the art was
  143. 5:52appended here to the end of the list now
  144. 5:54if we wanted to instead add art to a
  145. 5:56specific location in our list then we
  146. 5:59could instead use the insert method now
  147. 6:01insert takes two arguments first it
  148. 6:03takes the index where you want the to
  149. 6:05insert the value and then the value
  150. 6:07itself so if I wanted to insert art to
  151. 6:10the beginning of our list then we could
  152. 6:13say
  153. 6:14courses. insert and now the first
  154. 6:17argument is the location so let's just
  155. 6:19say location zero which is the beginning
  156. 6:21and then the value that we want to
  157. 6:23insert which is Art so if we run this
  158. 6:26now we can see that art was inserted at
  159. 6:28position zero
  160. 6:30now that only inserted the value it
  161. 6:31didn't overwrite anything so you can see
  162. 6:33that all the other courses are still
  163. 6:35here but they just got shifted now
  164. 6:37another way of adding values to our list
  165. 6:40is using the extend method now sometimes
  166. 6:42this confuses people so let's look at
  167. 6:44what this does so we want to use extend
  168. 6:47when we have multiple values that we
  169. 6:49want to add to our list so for example
  170. 6:52let's say that we have another list here
  171. 6:54called courses 2 and we'll set this
  172. 6:57equal to another list uh with art and
  173. 7:01education in this list and we want to
  174. 7:04add these values to our original courses
  175. 7:07list so first let's see what happens if
  176. 7:10we use this insert method so instead of
  177. 7:13inserting art to the beginning of our
  178. 7:15list let's instead insert these courses
  179. 7:18to the beginning of our list so let's go
  180. 7:20ahead and run this so we can see that at
  181. 7:22the beginning of our list that it
  182. 7:25actually added the entire list of
  183. 7:27courses to and not each each individual
  184. 7:30value so we can actually have a list
  185. 7:33within a list like we have here so if we
  186. 7:36were to print the first value of our
  187. 7:38courses list so I'll print out the index
  188. 7:41of zero and run that then we can see
  189. 7:43that now the first value is actually
  190. 7:46this list itself but this isn't really
  191. 7:48what we wanted we wanted to add all of
  192. 7:50those values from our second list to our
  193. 7:53original list now that is why we use the
  194. 7:55extend method so let's go ahead and set
  195. 7:58this back to the way was to just print
  196. 8:00the courses now instead of inserting
  197. 8:02here we'll instead use extend and it
  198. 8:05only takes one argument which is the
  199. 8:08iterable so we will extend courses with
  200. 8:12courses to so now if we run this then we
  201. 8:15can see that when we did courses. extend
  202. 8:18with courses 2 that it added the values
  203. 8:21from our second list uh here to our
  204. 8:24original so a lot of people get that
  205. 8:26mixed up with append and extend so again
  206. 8:30uh if you were to append it just like
  207. 8:31with insert then it's just going to
  208. 8:33append the list itself on there instead
  209. 8:37of the each individual item so if we use
  210. 8:40extend then now we can see that each
  211. 8:42individual item is extended onto that
  212. 8:45list okay so now let's look at how we
  213. 8:48can remove some items or remove some
  214. 8:51values from our list now one way to
  215. 8:53remove values is to just use the remove
  216. 8:56method so if we were to say courses do
  217. 9:00remove then let's say that we wanted to
  218. 9:02remove math so if we save that and run
  219. 9:05it then we can see that math was removed
  220. 9:08from the courses list now there's also a
  221. 9:10way of removing values with this pop
  222. 9:13method so if we say courses. pop Now by
  223. 9:17default this will remove the last value
  224. 9:19of our list now this is useful if we
  225. 9:22want to use our list like a stack or a
  226. 9:25CU so if we run this like it is then we
  227. 9:28can see that comp I was popped of off of
  228. 9:31our list and now we just have these
  229. 9:33three courses now one other thing about
  230. 9:35pop is that it Returns the value that it
  231. 9:38removed so we can actually set a
  232. 9:40variable uh and grab that returned value
  233. 9:43so if I set a variable here and say
  234. 9:45popped equals courses. poop and then I
  235. 9:49was to print this above our courses here
  236. 9:52and run that then we can see that it
  237. 9:55grabbed that compi value that was popped
  238. 9:57off of the list so if you had a or a
  239. 9:59queue then you could go through and just
  240. 10:02keep popping off values until your list
  241. 10:04is empty okay so now let's look at how
  242. 10:07we can sort our list in a couple of
  243. 10:09different ways here so first of all
  244. 10:11let's say that we just wanted to reverse
  245. 10:13our list as it currently is now this is
  246. 10:15pretty easy so we can just use the
  247. 10:17reverse method so I I can say courses.
  248. 10:20reverse and if we run this then we can
  249. 10:23see that now it prints out our uh
  250. 10:26courses but in Reverse so the last item
  251. 10:29all the way up until the first item now
  252. 10:31instead of reversing our list what if we
  253. 10:33wanted to sort our list now sorting is
  254. 10:36just as easy we can just use the sort
  255. 10:39method so I'll save that and run it and
  256. 10:42you can see that now our list is sorted
  257. 10:44in an alphabetical order so compi
  258. 10:47history math physics now if our list
  259. 10:49contained numbers then it would sort
  260. 10:51those in ascending order so let me call
  261. 10:54uh create another list here of numbers
  262. 10:57I'll just call that nums I'll set this
  263. 10:59equal to some random values so I'll say
  264. 11:011 5 uh 4 and three and we'll save that
  265. 11:05now below courses we will just say nums
  266. 11:08do sort and we will also print these out
  267. 11:11so I'll grab that variable print those
  268. 11:14out and run that so now we can see that
  269. 11:17our strings were sorted alphabetically
  270. 11:19and our numbers were sorted in ascending
  271. 11:21order now what if we wanted our values
  272. 11:24sorted in descending order now one way
  273. 11:26you might think to do this is just to
  274. 11:28use our reverse me method on the list
  275. 11:30after they're sorted and that would work
  276. 11:32but there's an easier way to do this uh
  277. 11:34instead we can just pass an argument to
  278. 11:37our sort method called reverse so if I
  279. 11:39come up here to the sort method and I
  280. 11:41pass reverse is equal to true and let me
  281. 11:45also grab this for our num sort and if I
  282. 11:49run this you can see that now these
  283. 11:51sorted are in descending order they're
  284. 11:54sorted in reverse order now one thing to
  285. 11:56notice here is that we don't need to
  286. 11:59reset our variables when we call most of
  287. 12:02these methods it's just altering the
  288. 12:05list in place but there's also a way
  289. 12:07that we can get a sorted version of our
  290. 12:09list without altering the original list
  291. 12:12so what if we wanted a sorted version of
  292. 12:15our courses list without uh altering the
  293. 12:19original so to do this we can use the
  294. 12:21sorted function so instead of calling
  295. 12:24this sort method on our list we'll
  296. 12:26instead use this sorted function and
  297. 12:29I'll pass courses in to sorted there now
  298. 12:32if we run this as it is then we can see
  299. 12:35that our list is not sorted it's exactly
  300. 12:37how uh we described it up here now
  301. 12:40that's because the sorted function
  302. 12:42doesn't sort the list in place it
  303. 12:45returns a sorted version of the list so
  304. 12:47to get that sorted list we have to make
  305. 12:50a new variable and set it to the return
  306. 12:52value of the sorted function so I could
  307. 12:55just call this sorted courses is equal
  308. 12:58to
  309. 12:59the sorted version of that course's list
  310. 13:03and now if we copy that and print that
  311. 13:05out so now we can see that this sorted
  312. 13:07courses is equal to the sorted version
  313. 13:09of that list so that's really useful
  314. 13:11because a lot of the times you won't
  315. 13:13want to alter your original list in any
  316. 13:16way um so using this sorted function is
  317. 13:18a nice way to get a sorted version of
  318. 13:20that list without altering the original
  319. 13:23and other than this sorted function
  320. 13:24there are a couple more useful built-ins
  321. 13:27uh that we can use with these SE quences
  322. 13:29so let's look at a few of these so we'll
  323. 13:31look at min max and sum so it's probably
  324. 13:35pretty obvious what these will do but if
  325. 13:37I wanted the minimum value of our
  326. 13:40numbers list here then let me comment
  327. 13:43out where we're printing out nums so a
  328. 13:45built-in function that we can use is
  329. 13:47just men and call men on our sequence of
  330. 13:50numbers and if I run that then you can
  331. 13:52see that it return returns one as the
  332. 13:54minimum number of that list and if we
  333. 13:56wanted the max value of that list then
  334. 13:58we could use Max and if I run that you
  335. 14:01can see it returns five um now if we
  336. 14:04wanted to print the sum of that entire
  337. 14:06sequence that I can just say sum of
  338. 14:09numbers if I print that then it gets 15
  339. 14:12because 1 + 5 + 2+ 4 + 3 is equal to 15
  340. 14:16okay so lastly let's see how we can find
  341. 14:20some values here within our list so let
  342. 14:22me go ahead and clean up here a little
  343. 14:25bit and uncomment out that so if we
  344. 14:28wanted to find the index of a certain
  345. 14:30value then we can use the index method
  346. 14:32for this so if I wanted to find the
  347. 14:34index of compi in our list then we could
  348. 14:38just print out
  349. 14:41courses. index and then search for compi
  350. 14:46now if we run this then we can see that
  351. 14:48we got three and three is the index
  352. 14:51where it found that compai value now if
  353. 14:53we try to find the index of a value that
  354. 14:56doesn't exist in the list then we'll get
  355. 14:58a value value errror so if we search
  356. 15:01that original list of courses for art
  357. 15:03and run that then we can see that we got
  358. 15:05a value error and it says art is not in
  359. 15:08the list now if we just wanted to Simply
  360. 15:11check if the value was in our list and
  361. 15:13simply get back a true or false result
  362. 15:16then for this we can use the in operator
  363. 15:19so if I wanted to check if art is in our
  364. 15:21list then I could say art in courses so
  365. 15:26if I run that and we can see that we got
  366. 15:28false but if instead we said math in
  367. 15:32courses and ran that then that returned
  368. 15:34true now this is going to be especially
  369. 15:37useful once we go over the topic of
  370. 15:39conditionals and if else statements and
  371. 15:41we're going to go over those in a couple
  372. 15:42of videos and we can also use this to
  373. 15:45Loop through values of our list by using
  374. 15:48a for Loop so if I was to say four item
  375. 15:53in courses and then scoot that over then
  376. 15:57we will just print out
  377. 15:59that item now this is the first time in
  378. 16:03our series that we've indented a block
  379. 16:05of code basically what we're saying here
  380. 16:07is that we want to create a loop We're
  381. 16:10looping through each value of our list
  382. 16:12and each Loop through this item variable
  383. 16:15will be equal to the next item in the
  384. 16:18list so that's why this line is indented
  385. 16:21because it tells us that this code is
  386. 16:23executed from within our for Loop so if
  387. 16:26we run this then we can see that it
  388. 16:28prints out each item of our list now the
  389. 16:31reason it prints them all out on a new
  390. 16:33line is because by default the print
  391. 16:35statement goes to a new line each time
  392. 16:37it's executed now this item variable is
  393. 16:40just a variable it's not a keyword or
  394. 16:41anything we can name this anything we
  395. 16:43want so instead if we wanted to you know
  396. 16:46call this course for course and courses
  397. 16:49and print that out then you can see that
  398. 16:50we get the same result so we can access
  399. 16:52each value as We're looping through but
  400. 16:55sometimes it might be useful to also
  401. 16:57have the index of what value were on Now
  402. 17:00to do this in Python we can access the
  403. 17:02index and the value by using the
  404. 17:05enumerate function so I will say
  405. 17:08enumerate and I will wrap courses within
  406. 17:11that enumerate function and this
  407. 17:13enumerate function returns two values it
  408. 17:15Returns the index that we're on and the
  409. 17:17value so instead of just getting the
  410. 17:19course here we're also going to need to
  411. 17:21get the index so I'll call that index so
  412. 17:24for index course in enumerate courses
  413. 17:28and now if we print out that index and
  414. 17:30the course and run that then you can see
  415. 17:33that we had access to each index and
  416. 17:36value as We're looping through our list
  417. 17:38and if we don't want to start at zero
  418. 17:41then we can pass in a start value to our
  419. 17:43numerate function so if instead we
  420. 17:45wanted to start at one then I can pass
  421. 17:50in the second argument say start is
  422. 17:52equal to one now if I run this then you
  423. 17:54can see that now our starting value is
  424. 17:57one okay so there is one one more thing
  425. 17:59that I want to go over before we move on
  426. 18:01to tupal and sets so it's pretty common
  427. 18:04that we'll want to turn our list into
  428. 18:06Strings separated by a certain value now
  429. 18:09to do this we're actually going to use a
  430. 18:11string method called join and we're
  431. 18:13going to pass in our list as the
  432. 18:15argument so for example let's say that
  433. 18:17we wanted to turn our list of courses
  434. 18:20into a string of comma separated values
  435. 18:22so we can say course string and that
  436. 18:25will hold the string version of our
  437. 18:27courses and now we'll type the string
  438. 18:29that we want to separate each item of
  439. 18:31our list so I want these to be comma
  440. 18:33separated so I'll put in a string of a
  441. 18:37comma and a space and then I'm going to
  442. 18:39use the join method to join the values
  443. 18:41of the list using this string so I'll
  444. 18:44say dojin and I want to join my courses
  445. 18:48on that string so now if I come down
  446. 18:50here and print that string version of
  447. 18:54our courses and run that then we can see
  448. 18:56that we get our values comma separated
  449. 18:58and if we want to change this then we
  450. 19:00can just change our string within uh
  451. 19:03that we're joining on so if we wanted to
  452. 19:05hyphenate these then I could just change
  453. 19:07this to space hyphen space and run that
  454. 19:10and you can see that when we ran that
  455. 19:12now we're separating these by The Hyphen
  456. 19:14so this is just one long string with all
  457. 19:17of our list values joined together now
  458. 19:19we can also do the reverse of this and
  459. 19:21turn a string back into a list so we can
  460. 19:24do this by splitting our string on a
  461. 19:26certain value so if I split the string
  462. 19:28that that we just created so let me
  463. 19:30create a new list here and I'll set this
  464. 19:33equal to the string version of our list
  465. 19:36and I'll do a DOT split and I'll split
  466. 19:39it on a space hyphen space so if we look
  467. 19:43at the string version that we have down
  468. 19:45here this is just one whole string what
  469. 19:47that's saying is it's saying hey split
  470. 19:49up all of these values on this space
  471. 19:51comma space and make a list out of all
  472. 19:53those values that you get so now if we
  473. 19:56print out our new list so I'll print
  474. 19:59that new list and run that and you can
  475. 20:02see that now we're back to the original
  476. 20:04list okay so I know that that was a lot
  477. 20:07to take in because there's so much that
  478. 20:08we can do with list but now let's move
  479. 20:11on to tupal and sets um now that we're
  480. 20:14familiar with list then these will
  481. 20:16probably go pretty quickly okay so tups
  482. 20:18are very similar to list but with one
  483. 20:21major difference so we can't modify
  484. 20:23tupal now in programming this is called
  485. 20:26mutable and immutable so list are
  486. 20:29mutable and tuples are not they are
  487. 20:31immutable so let's look at what this
  488. 20:33means so I'm going to grab a quick
  489. 20:35snippet of code here from my Snippets
  490. 20:38file uh just to compare these so I'm
  491. 20:40just going to grab from here all the way
  492. 20:43down to here and just go ahead and paste
  493. 20:47those in so first we have a regular list
  494. 20:50here that we've already looked at and
  495. 20:52we're going to look at one issue that
  496. 20:54you might run into with mutable objects
  497. 20:56so what I'm doing here is I'm creating
  498. 20:59our list of courses and I'm calling this
  499. 21:02list one and then we're creating another
  500. 21:04variable here called list two and we're
  501. 21:07setting that equal to list one and then
  502. 21:09we're printing out both of these so if I
  503. 21:12run this then we can see that both list
  504. 21:14one and list two have the same values so
  505. 21:17now let me uncomment out this code where
  506. 21:20we're changing a value in the first
  507. 21:23index of list one and then reprinting
  508. 21:26our values again so now if I run this
  509. 21:29then we can see here that all we did was
  510. 21:31change the value at the first index of
  511. 21:34list one but by changing list one's
  512. 21:37first value it also changed list two and
  513. 21:40the reason for that is because they're
  514. 21:42both the same mutable object now if you
  515. 21:45need to modify your list then this
  516. 21:47mutability is what you want but if you
  517. 21:50want a list of values that you no aren't
  518. 21:53going to change then we can use a tuple
  519. 21:55so let's look at a tupal example so I'm
  520. 21:58going to Common out our mutable example
  521. 22:00here and then uncommon out our Tuple
  522. 22:03example so a tupal looks almost exactly
  523. 22:06like a list but instead of the square
  524. 22:09brackets we're using instead using these
  525. 22:11parentheses and after we create our
  526. 22:13tupal we're doing the exact same thing
  527. 22:15we're calling a uh creating a variable
  528. 22:18Tuple one that is a tuple and then we're
  529. 22:21setting this Tuple two equal to Tuple
  530. 22:24one and for now I'll just comment out
  531. 22:26where we're changing that first value
  532. 22:28and I'll run that and you can see that
  533. 22:30both of these tupal are equal to the
  534. 22:33same list of values but now if we try to
  535. 22:36change that value at the first index of
  536. 22:39Tuple one just like we did with our list
  537. 22:42if we run that then we can see here that
  538. 22:44we get an error and it says type error
  539. 22:46Tuple does not support item assignment
  540. 22:49and that's because it's immutable um so
  541. 22:52now since a tupal is immutable it
  542. 22:55doesn't have nearly as many methods as a
  543. 22:58list because a lot of those list methods
  544. 23:00that we looked at involved mutating the
  545. 23:03values so we can't append we can't
  546. 23:05remove anything or anything like that
  547. 23:08but other than that they behave pretty
  548. 23:09much the same we can Loop through tupal
  549. 23:12we can access values and all the other
  550. 23:14things that we've already seen except
  551. 23:17for what mutates the list so that's
  552. 23:19basically the difference between list
  553. 23:21and tupal um if you need something that
  554. 23:23you can modify then use a list but if
  555. 23:26you just want something that you can
  556. 23:27Loop through and access then you might
  557. 23:30want to think about a tupal so that you
  558. 23:32don't run into the issues that we just
  559. 23:34saw okay and lastly let's look at sets
  560. 23:37now sets are values that are unordered
  561. 23:40and also have no duplicates so let me
  562. 23:43grab a sample from the Snippets file
  563. 23:45here um that we can see so I'm just
  564. 23:48going to go ahead and grab this small
  565. 23:50snippet and paste that in so we can see
  566. 23:52that this looks similar to lists and
  567. 23:54tupes but instead of the brackets or
  568. 23:57parentheses we're we're instead using
  569. 23:59these curly braces now if I run this
  570. 24:02code to print this set out then we can
  571. 24:04see that it prints out the values but if
  572. 24:06you notice here these aren't in the
  573. 24:07order that we added them so we had
  574. 24:09history first here and it has history
  575. 24:12last down here and if I run this a
  576. 24:14couple more times then we'll see that
  577. 24:16you know this order can change with each
  578. 24:19execution now the reason for this is
  579. 24:21because unlike our lists or tupal sets
  580. 24:24don't really care about order because
  581. 24:26some of the main uses for a set is
  582. 24:29either to test whether a value is part
  583. 24:31of a set and also it's used a lot to
  584. 24:33remove duplicate values because sets
  585. 24:35throw away duplicates so first let's
  586. 24:38look at how to get rid of duplicates so
  587. 24:40if I add another Math course here to the
  588. 24:43end of our set and now if I rerun this
  589. 24:47then we can see that we still only have
  590. 24:49four courses it got rid of that second
  591. 24:51Math course and just left us with the
  592. 24:54one now another thing I mentioned was
  593. 24:56that it's used to test whether a value
  594. 24:58is part of a set now this is called a
  595. 25:01membership test so sets do this a lot
  596. 25:03more efficiently than list and tupal so
  597. 25:06what I mean by this is that within our
  598. 25:08print statement if I was to say um math
  599. 25:13in CS courses and I run that then when
  600. 25:17you can see that it prints out true now
  601. 25:19we could do that with list and tups also
  602. 25:22but sets are optimized for this okay and
  603. 25:25lastly something else really useful that
  604. 25:27sets can quickly do is determine what
  605. 25:30values they either share or don't share
  606. 25:32with other sets so for example let me
  607. 25:35create another set called Art courses
  608. 25:38and I'll create this pretty similar to
  609. 25:40CS courses here um but instead of
  610. 25:44physics and compi I'll instead say Art
  611. 25:47and Design and with both of these I'm
  612. 25:49going to go ahead and take out that
  613. 25:51extra uh math on the end just so it's a
  614. 25:54little bit more clear and instead of
  615. 25:55this being CS courses I'm going to call
  616. 25:57this set art courses so now we have two
  617. 26:00different sets here and some of them
  618. 26:02have different courses and some of the
  619. 26:04courses are the same so let's say that I
  620. 26:06wanted to see what courses these sets
  621. 26:09had in common so to do this we could use
  622. 26:11the intersection method so we can say CS
  623. 26:16courses.
  624. 26:19intersection and we will pass in the art
  625. 26:22courses into that so if we run that then
  626. 26:25we can see that it shows us that the
  627. 26:27history and math coures are in both of
  628. 26:29those sets and if I wanted to see what
  629. 26:31courses are in the Cs courses but not
  630. 26:35the art courses then I could use the
  631. 26:37difference method so instead of
  632. 26:39intersection I will instead say
  633. 26:41difference so if I run that then now we
  634. 26:43can see that it shows that the uh
  635. 26:46physics and compsite courses weren't in
  636. 26:48the art courses now if I wanted to
  637. 26:51combine both of these sets and print all
  638. 26:54of the courses offered then I could use
  639. 26:56the union method so so I'll say CS
  640. 26:59courses. Union and we want to Union with
  641. 27:02the art courses so if I run that then we
  642. 27:05can see that now we get all of the
  643. 27:06courses printed out from both sets so
  644. 27:09sets can definitely be useful for these
  645. 27:11kinds of use cases and for these
  646. 27:13particular problems they're much more
  647. 27:15performant than lists or tupal okay so
  648. 27:18we are basically finished up with this
  649. 27:19video but let me show you one last thing
  650. 27:21before we close this out and that's
  651. 27:23going to be how to create empty list
  652. 27:25tupes and sets so I have these over here
  653. 27:28and my Snippets so let me grab these
  654. 27:31real quick and copy and paste these over
  655. 27:35here and bring this down a line so I
  656. 27:37wanted to show this because there's a
  657. 27:39small gotcha when it comes to creating
  658. 27:41an empty set so to create an empty list
  659. 27:45we can either set it equal to empty
  660. 27:47square brackets or we can use this
  661. 27:49built-in list class and to create an
  662. 27:52empty tupal we can use these empty
  663. 27:54parentheses or this built-in tupal class
  664. 27:57now to create an empty set we actually
  665. 27:59can't use these empty curly braces um so
  666. 28:03this line right here is wrong that's not
  667. 28:05an empty set this is actually going to
  668. 28:08create an empty dictionary so to create
  669. 28:10an empty set the way to properly do this
  670. 28:13is to use the built-in set class with no
  671. 28:16values and speaking of dictionaries
  672. 28:18that's actually what we're going to
  673. 28:19cover in our next video but I hope
  674. 28:22everyone feels comfortable now with
  675. 28:23working with lists and tupal and sets
  676. 28:26but if anyone does have any questions
  677. 28:27about about what we covered in this
  678. 28:29video then feel free to ask in the
  679. 28:30comment section below and I'll do my
  680. 28:31best to answer those now if you enjoy
  681. 28:33these tutorials and would like to
  682. 28:34support them then there are several ways
  683. 28:36you can do that the easiest way is to
  684. 28:38Simply like the video and give it a
  685. 28:39thumbs up and also it's a huge help to
  686. 28:41share these videos with anyone who you
  687. 28:42think would find them useful and if you
  688. 28:44have the means you can contribute
  689. 28:45through patreon and there's a link to
  690. 28:47that page in the description section
  691. 28:48below be sure to subscribe for future
  692. 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.