Intermediate Python Slicing Lists
Transcript from the "Slicing Lists" Lesson
>> Nina Zakharenko: Python doesn't really know how to represent this generator in any other way. It doesn't know what's in it until you start looping through it. The other cool thing about generators is they don't necessarily have to have an end, right? The structure is not in memory, so you can kind of be a little loosey-goosey about it.
[00:00:20] A good way to think about generator is the list has all the items in it. But a generator, it only knows where it is right now, and it knows the very next step. It has no idea what happened in the past. It's got some weird amnesia. And it doesn't really know where it's going in the future besides for the very next step.
[00:00:42] Does that make a little bit more sense? Okay, we talked a little bit about slicing yesterday, but slicing is a way to create sublists from larger lists. So let's say I had my_string. So a string is kind of considered a sequence in Python.
>> Nina Zakharenko: What will my_string(0) return?
>> Speaker 2: 'H'.
>> Nina Zakharenko: 'H'.
>> Nina Zakharenko: We also looked at negative slicing. What will my_string[-1] return?
>> Speaker 3: !.
>> Nina Zakharenko: !, but we also learned that we can provide two numbers into this range. So let's just say I wanted to return the string hello. I can say zero, one, two, three, four.
>> Nina Zakharenko: And I would put in position 5 because just like range,
>> Nina Zakharenko: These are the first four characters just like range. The second argument to the subscript it's exclusive, so it does not include the character that you put in there. We also know that when you're working with either the first value of the list or the last value of the list, that the 0 is optional.
[00:02:20] It gives us the same thing.
>> Nina Zakharenko: So we can put any numbers in here that we want. It'll give us a new copy of those values from the string. So doing it with a list,
>> Nina Zakharenko: Renders the same results. Say I have a list of four items, what we'll name 0 return?
>> Speaker 3: Nina.
>> Nina Zakharenko: Nina, and names[-1].
>> Speaker 4: Jimmy.
>> Speaker 5: None. Nope, Jimmy.
>> Nina Zakharenko: Jimmy, it's the last item. What if I wanted the first two items in the list? What would I put for my second range variable?
>> Speaker 3: 1?
>> Nina Zakharenko: First two items.
>> Speaker 2: 3?
>> Nina Zakharenko: Would be 3.
>> Speaker 4: 2?
>> Nina Zakharenko: I'm sorry, 2, whoever corrected me, you were right because list indexes start at 0, okay. And is this 0 optional or not?
>> Speaker 4: It is.
>> Nina Zakharenko: It is, okay. If I wanted to go to the end of the string, I would start at 0, 1, 2, and I would go to,
>> Nina Zakharenko: I would go to 4. So that's the end of my list, that's the length of it. Just like with 0, is this 4 optional?
>> Speaker 2: Yes.
>> Nina Zakharenko: It is, cuz we're going all the way to the end, okay.
>> Nina Zakharenko: One of the cool useful uses for this substring, so if we have a list, let's say my list names, and I reassign it to new_names.
[00:04:16] If I append a new value here, is it gonna change my names list?
>> Nina Zakharenko: Will it?
>> Speaker 3: Yes.
>> Nina Zakharenko: It will, right, cuz all I did is I changed the label here. I didn't make a new list, right? I just told Python these things share a name now.
[00:04:42] They're pointed at the same thing, so-
>> Speaker 4: Can you print out names too?
>> Nina Zakharenko: It's the same. So if I quickly wanted to make a copy of my list so that I can do some operations on it and not modify the original, let's say I had my list of names here.
>> Nina Zakharenko: If we start at the beginning of a sequence and we wanna take a slice, is 0 optional?
>> Speaker 4: Yes.
>> Nina Zakharenko: Yes, okay, if we go to the end of the sequence, is the last number, that last value, is that optional? It is, so if I put just the : here, I'll get a copy of my list.
[00:05:34] And now I can say, new_names is equal to a copy of my names list. Sorry, we got news_names but bear with me. If I append something to this now,
>> Nina Zakharenko: Will my names list change? No, but my news_names I modified that, right?
>> Nina Zakharenko: Okay, let's talk about negative indexing a little bit.
>> Nina Zakharenko: So I had my_string here, "Hello World!".
>> Nina Zakharenko: If I do a negative index [-1] what do I get?
>> Speaker 5: Exclamation point?
>> Nina Zakharenko: What about negative index [-2]?
>> Speaker 5: d.
>> Nina Zakharenko: It's a d, right. See where I'm getting here?
>> Speaker 5: Kinda like it's circular.
>> Nina Zakharenko: Yeah, so -1 goes to the first last item, -2, kind of etc, etc.
[00:06:45] You can get kinda interesting here. You can go from the end, the length of the string, to the fourth from the end and end up with some subset of the string. You can do the same thing for lists. You kinda rarely want to get too tricky with the substrings because explicit is better than implicit.
[00:07:13] You want to be clear. But it's available to you, and you might see this in Python code.
>> Nina Zakharenko: Okay, one last argument. Let's say I had a list of the numbers from 0 to 9. I can quickly build that list with range, right, and pass it into list and just looking at our list, the numbers from 0 to 9.
>> Nina Zakharenko: We know that this will give us the whole list. There's one additional of third argument to step, and you can kinda think about it like range. So the first argument to range, the optional first argument, is the start. The second argument to range is the end. What's the third optional argument to range?
>> Speaker 2: Step.
>> Speaker 5: Interval.
>> Nina Zakharenko: It's a step, yeah, interval or a step. So I can do the same thing here and say, just give me every other one.
>> Nina Zakharenko: One little other bit of trickery here, what if the step was -1, what do you think that would do?
>> Speaker 5: Tell it backwards?
>> Nina Zakharenko: That's right, yeah. So going through the whole list, stepping from the end to the beginning. So just a little bit of trickery there. Let's say I wanted to,
>> Nina Zakharenko: Go start from the first value and let's look at my list again here. I wanted to start from the first value, go to the sixth one, and then skip every other.
[00:09:03] I could use all three of those arguments to range.
>> Nina Zakharenko: And remember that you can slice to get a subset of items from any data type that maintains an order. So that would be your list and your tuple. Can you do this sort of sub sequencing with a dictionary or a set?
[00:09:30] No, right, why?
>> Speaker 2: Unordered.
>> Nina Zakharenko: They're unordered, right? So for the data structure, if you can't subscript it by an index, and remember, you can do this with the dictionary, only if the dictionary has a key of 0, so that doesn't count. If you can do this to the data type, then you can take a slice of it.