00:00Previously, I showed you that in order to get the index of an item, you can use the
00:11index method.
00:12Now, how can we do the reverse?
00:15How can we get an item given its index?
00:19Well, for that, we need to look one more time at the list of attributes that we can apply
00:26to list.
00:27And I know that we can use the underscore, underscore, get item, underscore, underscore
00:33method.
00:34Okay, I know, I told you earlier that these methods rarely you're going to use.
00:40And in fact, we don't need this method for what I'm going to do now.
00:45But let me explain why in just a bit, you'll understand, and this is a very secret of Python.
00:52So let me go ahead and use get item with double underscores.
01:00And of course, it's a method.
01:01So it needs this round brackets, parentheses.
01:06So let's try to get the item with index one, which would that be?
01:13Well, 8.8.
01:14Now, the secret is that you don't have to use that ugly method there with those double underscores.
01:24All you have to do is use this syntax.
01:27So call the variable name, like that, and then add open and closing square brackets.
01:34And then inside them, just enter the index.
01:37And that will give you the value, the item for that index again.
01:41Even though you're doing this, Python on the background actually is calling this method,
01:49and is passing this item, this value as an input for that method.
01:56So this is actually what's going on on the background.
01:59But Python lets you do it in a better fashion, like this in here.
02:11I have a list of five items here.
02:14Let me show you why.
02:17Use the len function, and you see that it has, it really has five items.
02:23Now besides accessing single items out of your list, like that item with index 3, 0, 1,
02:322, 3, so 6.6, you can also access portions of the list.
02:41Let's say from 1 to 4.
02:46That will give you another list.
02:49So the output of this is also a list.
02:53And it's going to contain the items with index 1, index 2, and index 3.
03:01The upper limit, it's not included.
03:04It's never included in a Python slice.
03:07So this is a slice.
03:09And this is the output of the slice.
03:11Let's try to get the first two items of the list.
03:14The first two items have an index of 0 and 1.
03:19So we'll start with 0.
03:21Since we also want the item with index 1, we're going to write 2 there.
03:26So that we get 1, and that is the output.
03:31So the index 2 is not included, 7.5 is not included.
03:37You can also use a shortcut for that.
03:39You don't have to type 0 if you meant to get everything starting from the first item.
03:47And that will give you the same output.
03:49Similarly, if you want to get, let's say, the last two items, you can start to count 0, 1, 2, 3.
04:00So 3, start from 3.
04:03And this has an index of 4.
04:06So we're going to write 5 there.
04:10Even though we got the expected output, you'd better be using the shortcut for that.
04:18So this is more intuitive because it tells you that you're extracting from item with index 3 to the last item of the list.
04:27So without knowing how many items there are in the list.
04:31Similarly here, you're extracting from the very first item to the item with index 1.
04:37Now, for big lists, when you want to extract the last items, it may make not much sense to count from the beginning.
04:45Like 1, 2, 3, 4, 5, 6, and so on.
04:49But Python has another system of indexing, and I'll show you that in the next video.
04:55So how do we get the last item of this list?
05:04Well, you can do that by referring to the list and then square brackets and then refer to the index of the item you want.
05:12So 9.9 has an index of 0, 1, 2, 3, 4, 4.
05:17Okay, but a smarter way is to use negative indexing, which means instead of counting, you can just write minus 1 and you get 9.9.
05:30So each of the items of a Python list has two indexes, a negative and a positive index.
05:36So minus 1 for this, minus 2 for that, minus 3, minus 4, minus 5.
05:42So minus 5 is the first item actually, 9.1.
05:48And of course, you can do list slicing using negative indexing.
05:54So let's try to get the last two items.
06:00So to do this slicing, you have to put the index of the first item of the slice.
06:06So the first item of the slice is 6.6 and 6.6 has a negative index of 2.
06:14Use a colon and then you don't want to assign anything here because you want everything that comes after 6.6.
06:22So just like that, and you get the tail of the list.
06:26So similarly, you can do minus 4, minus 2.
06:30For example, that will give you 8.8, minus 4, minus 3 and minus 2 is not included in the slice
06:41because I told you that the upper limit is never included in the slice.
06:48So you only get this and that.
06:50You can also start from the beginning of the list to get 9.1, the first item, like that.
07:01To your surprise or not, strings also have an indexing system.
07:07The good news is that they have exactly the same system as lists.
07:11So they have positive and negative indexing and you can slice them and you can access items from them.
07:16So let me make a quick demonstration, string hello.
07:24That will give you the string e, which is still a string.
07:29Hello is a string, e is also a string.
07:32And negative indexing.
07:35And also slicing.
07:36One cool thing you can do is, let me get this list.
07:40So it's a list containing different types of objects.
07:45And what you can do is, you can access.
07:50Guess it.
07:51Well, that's the hello string.
07:52So the first item of the list.
07:54And then you can use a chain indexing such as, for example, if you type 2 there, guess what you're going to get?
08:04Well, you're going to get the first L because that has an index of 2.
08:09So 0 for h, 1 for e, 2 for l.
08:15Because this part here is a value on its own.
08:19And this other part will get the item with index 2 of that value, which happens to be a string.
08:26It is as simple as that.
08:28Do dictionaries have the same index system as lists and strings?
08:37Let's find that out.
08:39I have this dictionary here on my Python shell.
08:43Student grade.
08:44So let's try to get the second item.
08:47Maybe?
08:50Nope.
08:51It says key error 1, which means that the dictionary doesn't have a key named 1.
09:01So what keys does dictionary have?
09:04Maybe the same key?
09:07Yes.
09:08So dictionaries, instead of integers, they have keys as indexes.
09:13So a list would be accessed, the items of the list would be accessed via integers,
09:20which start from 0 and end to the last item of the list, the index of the last item of the list.
09:27With dictionaries, this is different, as you can see.
09:29And this is actually what makes dictionaries a dictionary.
09:33Let's see another example of how useful this is.
09:36Let's say you have this dictionary.
09:37English to Portuguese.
09:39It's a real human dictionary.
09:42You have some words there.
09:44Let's say rain, shuva in Portuguese, and sun would be sol.
09:56Sorry, I've got a typo there.
09:59Quote.
10:01So now if you want the Portuguese version of sun, you write that.
10:07And that will give you that version.
10:09So having integers as indexes, in this case, would be very useless.
10:14So every use case has its own data structure to be used in Python.