#100DaysOfWeb in Python Transcripts
Chapter: Appendix: Python language concepts
Lecture: Concept: Slicing
Login or
purchase this course
to watch this video and the rest of the course contents.
0:01
Python has this really interesting concept called slicing. It lets us work with things like lists, here in interesting ways.
0:08
It lets us pull out subsets and subsequences if you will, but it doesn't just apply to lists,
0:14
this is a more general concept that can be applied in really interesting way, for example some of the database access libraries,
0:21
when you do a query what you pulled back, you can actually apply this slicing concept for eliminating the results
0:28
as well as paging and things like that. So let's look at slicing. We can index into this list of numbers like so,
0:35
we just go to nums list and we say bracket and we give the index, and in Python these are zero-based, so the first one is zero,
0:41
the second one is one and so on. This is standard across almost every language. However, in Python, you can also have reverse indexes
0:49
so if I want the last one, I can say minus one. So this is not slicing, this is just accessing the values.
0:54
But we can take this concept and push it a little farther. So if I want the first four, I could say 0:4
1:02
and that will say start at the 0th and go up to but not including the one at index 4. So we get 2, 3, 5, 7, out of our list.
1:10
Now, when you are doing these slices, any time you are starting at the beginning or finishing at the end,
1:16
you can omit that, so here we could achieve the same goal by just saying :4, assuming zero for the starting point.
1:22
So, slicing is like array access but it works for ranges instead of for just individual elements. Now if we want to get the middle,
1:31
we can of course say we want to go from the fourth item, so index 3, remember zero-based, so 3 and then we want to go up to
1:38
but not including the sixth index value, we could say 3:6 and that gives us 7, 11 and 13.
1:45
If we want to access items at the end of the list, it's very much like the beginning, we could say we want to go from the sixth element so zero-based,
1:55
that would be 5 up to the end, so 5:9 and it would be 13, 17, 19, 23,
2:00
but like I said, when you are either starting at the beginning or ending at the end,
2:04
you can omit that number, which means you don't have to compute it, that's great, so we could say 5: and then it'll get the last one.
2:10
But you still need to know where that starts, if we actually wanted 4, so there is a little bit of math there,
2:16
if you just want to think of it starting at the end and give me a certain number of items,
2:20
just like where we got the last prime and that came back as 23 when we gave it a minus one, we can do something similar for slicing
2:27
and we could say I'd like to go start 4 in from the back, so negative 4 and then go to the end.
2:33
So that's the idea of slicing, it's all about working with subsets of our collection here, the example I gave you is about a list,
2:40
but like I said we could apply this to a database query, we could apply this to many things in Python
2:46
and you can write classes that extend this concept and make it mean whatever you want,
2:50
so you'll find this is a very useful and common thing to do in Python.