Discover how to efficiently manipulate Python lists by saving modified loop outputs and performing precise slicing operations. Enhance your programming skills by mastering list access methods and effective data handling strategies.
Key Insights
- Create and save new modified lists by initializing an empty list, looping through existing data, concatenating strings, and using the append method, ensuring results are stored and reusable within the program.
- Utilize Python's slicing syntax—[start:stop:step]—to selectively extract items from lists, such as skipping the first three and last three items or retrieving every other item, enhancing your ability to manipulate data precisely.
- Access individual list items and ranges effectively by employing indexing methods like negative indexing (for example, [-1] for the last item) and the index method to locate specific elements such as "kiwi jellybean."
Note: These materials offer prospective students a preview of how our classes are structured. Students enrolled in this course will receive access to the full set of materials, including video lectures, project-based assignments, and instructor feedback.
This is a lesson preview only. For the full lesson, purchase the course here.
We're going to loop through the fruit list again. We're going to do another fruit loop: for fruit in fruits
. And every time through, we're not just going to print the fruit—in other words, fruit
—we're going to print the fruit plus the word "jellybean." And there you go.
Next we're going to make jellybeans again. This idea of printing works, but be careful. It's not as programmatically useful to simply print everything. I mean, you would run a loop on data for some reason, right? You're trying to do something—maybe get some new data result.
We have these jellybeans, but we're not storing them anywhere. They're not being saved to a new list or anything. So even though they're coming out here in the window—this little print window—they're not available in the program anywhere.
We can't look up some jellybean later, like pick a random jellybean or something. They're not being saved. So better would be to make jellybeans again, but this time save them to a new list.
We're going to make a new empty list by saying list = []
. We'll say jellybeans = []
.
Now we'll do our loop. We'll say for fruit in fruits
again, but rather than print the jellybean every time—you don't want to do that—we're going to go to jellybeans.append()
and append the jellybean. Then print the jellybeans list when we're done.
So start with an empty list, loop through all the fruits, make jellybeans, CONCATENATE the string (current fruit plus " jellybean"), take that string result, and append it into this new list, which will just grow. And you'll have one jellybean per fruit, just like before—except the big difference is they exist in a list now. There it is.
And note the wrapper square brackets that you do not have when you just print. All right, let's print all but the first three and last three items. Okay.
A little reminder: we don't want to print the first three and we don't want to print the last three. We're going to say: start at (skip) 0,1, and 2.
We're going to start at 3, and we don't want the last three. So skip -1, -2, and -3. So let's turn off this print.
There we go. It's all but the first and last three. Here's an every-other move.
Start, stop, and step. Remember when we were doing that SAT score—we had a start of 400 and an end of 800—but we had a step of 10 because we wanted increments of 10. You can use increments of 2.
Like, "I want every other jellybean." So you would say: start—here, let's skip the first three and the last three—and then add another argument called the step.
Oh, that's got to be a colon. Okay. Whoa.
Oh, can I have a negative? Oh, it's P. We need pp. Okay, here, let's turn this one off.
There. That's just every other one—skipping the first three and the last three.
Yes. Skip the first three and last three items, print every other jellybean, and then just print every other jellybean starting from the beginning to the end. If you want to start at the beginning, you could put a 0, but remember—you don't have to.
And if you want to go to the end, you put nothing. So you would take out the value there. Basically, the little trick move is: that is the move for getting every other item.
So that's the slice move. It's list[::2]
—that's how you get every other item. We're doing a nice little review of not just list methods, but also slicing—selecting multiple list items.
So that would be every other jellybean. So let's loop with an increment of every other item. What we're going to do is this: jellybeans[::2]
—that’s what we’ll iterate over.
We're going to say for fruit in jellybeans[::2]
. And then we'll just print—no, look, that's not a fruit.
That's a jellybean at this point. We'll just print the current jellybean (JB). That's every other jellybean.
Print every other jellybean in a loop. Now, why would you want to use a loop when you could just print every other jellybean directly like we just did? Well, because you might want to perform additional actions. Maybe you want to focus on every other jellybean and then add some conditional logic or something.
Let's keep rolling. Here’s another challenge review—this one focuses on accessing list items, right? Using square brackets, selecting items like we've been reviewing here, as well as list methods.
We had a nice challenge of list methods. This is a challenge of accessing list items in a range or an individual item. Okay, let's start by printing the first jellybean, then the last jellybean, then the first five, then the last four.
Starting with kiwi jellybean, print it and the next five. Print all but the first and last jellybeans. Try it out.
It's review, but it's a challenge. Pause, give it your best shot, come back, and we'll do the solution together. Okay, so we're going to say jellybeans[0]
, right? That's the first item.
And the last item will be jellybeans[-1]
. The first five items will be jellybeans[:5]
. The last four items will be jellybeans[-4:]
.
Starting with kiwi jellybean, print it and the next five. Well, you'd have to know where the kiwi is. We'd say index_of_kiwi_jb = jellybeans.index("kiwi jellybean")
.
We could print that, just to make sure. Let's print everything up to this point. There's your first jellybean, right? Print the first jellybean—that’s the apple.
Print the last jellybean—that’s your watermelon. Print the first five—there you go. Print the last four—there you go.
Get the index of the kiwi jellybean. Now that we have the index, print it and the next five. Where are we starting from? We're starting from index_of_kiwi_jb.
We're starting from 9, then going to 14 (exclusive), which gives us 6 items. We actually have to do "kiwi jellybean"
again, because we don't know what it is.
Then say plus 6—that should give us all six. Kiwi plus the next five—that would be six in total.
Kiwi jellybean plus next five—there it is. And by the way, if you want to use ppprint
for clarity, you could do that, but you can’t include a string label when using ppprint
.
If you try to ppprint
two things at once—like the label and the values—it will cause an error. If you want to ppprint
, include no extra value—no labeling of your data there.
So there's kiwi plus the next five. One little move is: if you do a \n
before you print, it'll give you a line break. It's kind of cool.
If you want some visual separation, you can do another line break after that, depending on what you want to show next.