If You’re Starting to List, How Bout a Slice? 

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[0] = ‘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:

>>> pokerBuddies[0:2]

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

So, After Tupling, When Do You Start to List? 

On the crucial differences between tuples and lists in Python

Listing is sort of like tupling, except lists are more loosey goosey. The stuff in a tuple is more solid than rock. It can’t be changed, not with a chisel, a jackhammer or bunker-busting bomb (however, see the postscript below for a loophole).

Lists, in contrast, are more flexible than your average yogi.  The data in a list can be changed, and the lists themselves can grow longer or shorter at will (or close to it).

You recognize a list because it comes in brackets rather than parentheses. So, here’s an example of a list:

thisList = [“Joe”, “Shiho”, “Jose”]

What’s cool about the list is that we can swap elements in or out. Let’s say Joe is just getting too gross for the poker-playing gang, with his jalapeno nacho breath and his cheesy fingers. We dump Joe from the list and substitute in Sarah, who makes an awesome bean dip. She’s on the list now, and Joe is out.

Here’s how you could do that in Python:

thisList = ["Joe", "Shiho", "Jose"]
print (thisList)

That will print your new list, the one with Joe gone and Sarah added::

 ["Shiho", "Jose", "Sarah"]

If you try to do the same thing with a tuple, it won’t work. Therefore, if you need some flexibility, go with the list. If you need tight, anal-compulsive security, go with the tuple.

PS: If you want some more information on lists, I recommend TutorialsPoint.

PPS: Some experts have noted that tuples are immutable but not necessarily unchanging. Yeah, I know that sounds like a contradiction. To learn about it, though, have a look at “Python tuples: immutable but potentially changing”.

Features image: Brazilian poker player Maria Eduarda "Maridu" Mayrinck during the World Series of Poker, 2009, Author: Juliano Maesano. From https://commons.wikimedia.org/wiki/File:Maridu.jpg