On how to slice Python lists and why it’s so helpful
When I think of a slice in the real world, I tend to think pizza. In the world of programming, however, I think of lists. And, like pizzas, they’re just begging to be sliced up.
In fact, you could argue that slicing and dicing data is what computing is all about. In the Python coding world, lists are the pizzas (or maybe the loaves of bread or pieces of pie or whatever you fancy). As when you’re slicing up your favorite food, however, you’ve got to watch out for your digits.
Let’s harken back to our original list of poker buddies:
>>>pokerBuddies = ["Joe", "Shiho", "Jose"]
Go ahead and type that into your IDLE Shell and hit Enter.
Now, before we discuss slicing, I’ve got to tell you another weird thing about Python. It’s has an annoying habit. Let’s say Python holds up its proverbial finger and asks you what number it is. You say, “One, of course.” Python says, “No, dummy, it’s zero.”
That is, if you want to count to three in Pythonese, you count, “Zero, one, two, three.”
Here’s how that affects slicing. If you want to slice out the first name in the list above, you write:
Put that into your Shell and hit Enter. What the Shell should spit back out at you is:
That’s because you have sliced out the first item in the list (which is actually the 0 item in the list). That number represents what we call the slicing index, or just index.
Now try this:
This time you should see:
Obviously, you get the second item in the list. As long as you count up from zero, it’ll be okay.
In the previous post, we showed you how you could remove a name from a list and add a different name. Here’s another way you can do it via slicing:
>>>pokerBuddies = ‘Sarah’
What you’re doing is assigning the first position in the list to Sarah instead of Joe (Joe, as you’ll recall, has a nacho breath problem). Now, your list reads:
['Sarah', 'Shiho', 'Jose']
Okay, those are the basics. There’s oh-so-much more to this topic. In fact, some people might even object to calling this simple stuff “slicing.” Slicing, they might legitimately argue, involves taking more than one piece out of the pizza. For example, you can cut more than one name out of the list above. Here’s how:
This slices two names out of the list. Do you see that second index number, which is 2? Weirdly, Python requires that the second number be the number just beyond the last numbered item you want to slice. That is, if you want to slice out the first two names, you do not use, as you might expect, [0:1]. Rather, you use [0:2]. If you fool around with this list and longer lists, you’ll start to get the idea.
If you want more information on list slicing and lists in general, I recommend Dive Into Python and How to Think Like a Computer Scientist.
Feature image: A sign for pizza by the slice at a restaurant in San Francisco, California. Matthew Dillon from Hollywood, CA, USA - Pizza By The Slice. Go to https://en.wikipedia.org/wiki/Pizza_by_the_slice#/media/File:Pizza_By_The_Slice_sign.jpg