#
Python Jumpstart by Building 10 Apps Transcripts

Chapter: App 5: Real-time weather client

Lecture: Concept: Slicing collections

Login or
purchase this course
to watch this video and the rest of the course contents.

0:00
Let's talk about slicing.

0:02
You can slice all sorts of things, you can slice strings,

0:05
you can slice lists, you can even slice results that come back from databases

0:10
you can have that slicing due some kind of paging on the database side on the server,

0:16
it's super powerful and it's one of these concepts

0:19
I would classify as Pythonic meaning that this is a programming idiom

0:23
or style of programming that is somewhat unique to Python.

0:27
So here I have a list of numbers which I've called nums

0:30
and it happens to be the first 9 prime numbers,

0:33
I can do things that are really obvious like get the first prime

0:36
by saying nums [0],

0:39
remember, accessing elements in lists and all sorts of things in Python is zero based

0:44
so it starts counting at 0, 1, 2, 3, so the first one would be nums of 0,

0:47
and you can see on the comment on the right that would return 2,

0:51
something we haven't talked about which is also fairly unique to Python

0:55
is that you can use the negative indexes,

0:57
so instead of having to somehow do some math,

1:00
taking the length of the numbers into account

1:02
and subtracting 1 from them, to get the last item, we can actually use

1:06
here we can use minus 1 and that will give us the last number

1:10
so if we put minus 1 in there we get 23,

1:13
but those are just regular array indexes,

1:15
however if we give it a range separated by colon then we get slices,

1:21
so if we want the lowest four prime numbers

1:24
we would say as you saw in our HTML example 0:4

1:27
and that gives us the first 4 items,

1:29
but there is the shortcut here where we can omit

1:32
the beginning if we are starting at 0,

1:34
or the end, if we are going all the way to the end,

1:36
so these two statements are equivalent

1:39
we can also get the middle ones so if we want the 4th or the 3 index 0 based item

1:45
and then 3 more, we can say 3:6 and that will give us 7 through 13,

1:51
and we can also go to the end just like our sliced one

1:54
from the beginning so we could if we computed the actual link

1:57
that for some reason we knew it because a 5:9

2:00
now in programs this is of course very dynamic

2:03
you rarely have a fixed link you just hard code in there,

2:07
so this would be something like the length of numbers

2:10
minus 4: the length of numbers this is not cool,

2:13
but it does give us the result we are looking for,

2:15
what is more cool is to just say I would like to go from the 5th item onward,

2:20
that still requires you to know the length of numbers

2:23
if you want just the last 4, so we can use this idea

2:26
of a negative index kind of like I talked about

2:29
for indexing here but for ranges,

2:31
and we can say I don't know how long it is

2:33
but I want you to go to the end and then go back for

2:36
and then go all the way to the end,

2:38
so this - 4: nothing will actually give us the last 4 items without

2:43
us caring what the length is.

2:45
It's worth noting that when you are doing these slices

2:47
this type of stuff here you don't get errors

2:52
if you use numbers that are too big or too small.

2:54
So if I said 5:1000 and there is only 10 items,

2:57
it would just give me the last 5

2:59
but up here where I am using array indexes

3:01
if I say give me the 1000th item and there is only 10- boom, that's a crash.

3:06
So these aren't quite treated the same way in that regard,

3:09
but otherwise you can think of using these ranges as indexes

3:13
into the list and what you get back are little smaller lists.