Python List Slicing

In the last lesson you learned List Methods, the built-in tools that change a list in place. Now here is a different need. You don’t always want the whole list. Sometimes you want just a part of it. The first three names. The last two scores. Every second item. That’s what slicing does. The good news is you already know it.

🤔 Why List Slicing?

Say you have a list of high scores and you only want the top three. You could write a loop, count to three, and copy each one into a new list. That works. But it’s a lot of typing for something so simple.

Slicing solves this in one line. You tell Python where to start and where to stop. It hands you back a new list with just that part. No loop. No counting by hand.

🧩 It’s the Same as String Slicing

If you remember slicing a string, this is the exact same idea. Back then you took part of some text. Now you take part of a list. Same brackets. Same rule. The only difference is what’s inside.

The shape looks like this:

my_list[start:stop:step]

Here is what each part means:

  • start is where the slice begins. Python keeps this index.
  • stop is where it ends. Python stops before this index. It does not include it.
  • step is how big the jump is. Leave it out and it’s 1, meaning one item at a time.

The big thing to remember is that stop is not included. So [2:5] gives you items at index 2, 3, and 4. Not 5. It is easy to forget this at first, so keep it in mind.

✂️ Grabbing the First Few

Let’s start with a list of names and pull out the first three.

names = ["Alex", "Riya", "Arjun", "Sara", "Mia"]
first_three = names[0:3]
print(first_three)

The slice [0:3] starts at index 0 and stops before index 3. So you get the items at 0, 1, and 2.

Output

['Alex', 'Riya', 'Arjun']

There’s a small shortcut here. If you start from the very beginning, you can drop the 0 and just write [:3]. Python assumes you mean “from the start”.

names = ["Alex", "Riya", "Arjun", "Sara", "Mia"]
print(names[:3])

Output

['Alex', 'Riya', 'Arjun']

Tip

Read [:3] out loud as “the first three”. And [3:] as “everything from index 3 onward”. Saying it as words makes the numbers easier to remember.

👉 Grabbing the Last Few

Leaving the stop empty means “go all the way to the end”. So [2:] gives you everything from index 2 onward.

names = ["Alex", "Riya", "Arjun", "Sara", "Mia"]
print(names[2:])

Output

['Arjun', 'Sara', 'Mia']

But what if you want the last two and you don’t want to count how long the list is? Use negative numbers. Negative indexing counts from the end. So -1 is the last item, -2 is the one before it, and so on.

This grabs the last two no matter how long the list gets:

names = ["Alex", "Riya", "Arjun", "Sara", "Mia"]
print(names[-2:])

The slice [-2:] means “start two from the end, go to the end”. So you get the final two items.

Output

['Sara', 'Mia']

⏭️ Taking Every Second Item

Now the third number, the step. The step says how far to jump each time. A step of 2 means take one, skip one, take one, skip one.

Here we grab every second number from a list:

numbers = [10, 20, 30, 40, 50, 60]
print(numbers[::2])

The two empty slots before the step mean “from the start, to the end”. The 2 then jumps two at a time. So you get the items at index 0, 2, and 4.

Output

[10, 30, 50]

Notice the pattern [::2]. Both start and stop are empty. So it walks the whole list, just two steps at a time.

🔄 Reversing a List

Here is a useful trick. A step of -1 walks the list backward. So [::-1] gives you the whole list reversed, in one move.

numbers = [10, 20, 30, 40, 50, 60]
print(numbers[::-1])

The empty start and stop say “the whole list”. The -1 step then reads it from the back to the front.

Output

[60, 50, 40, 30, 20, 10]

Note

Slicing never changes the original list. It always hands you back a new list. So after numbers[::-1], the variable numbers is still in its original order. If you want to keep the reversed version, save it to a variable.

🔧 Replacing a Chunk with Slice Assignment

So far slicing has been about reading parts out. But you can also slice on the left side of an = and swap a whole chunk for new values. This is called slice assignment.

Say two names in the middle need replacing:

names = ["Alex", "Riya", "Arjun", "Sara", "Mia"]
names[1:3] = ["Ben", "Noah"]
print(names)

The slice [1:3] points at the items “Riya” and “Arjun”. The assignment drops in the new values in their place.

Output

['Alex', 'Ben', 'Noah', 'Sara', 'Mia']

The new chunk doesn’t even have to be the same size. Python stretches or shrinks the list to fit:

numbers = [1, 2, 3, 4, 5]
numbers[1:4] = [99]
print(numbers)

Here three items get replaced by just one, so the list gets shorter.

Output

[1, 99, 5]

⚠️ Common Mistakes

A few common mistakes catch new learners at the start.

One common mistake is expecting the stop value to be included. [2:5] does not include index 5. It stops just before. When in doubt, count the indexes on paper.

Another is forgetting that a slice is a copy. Reading a slice never touches the original list. If you need the result, save it. For example, top = scores[:3].

# ❌ Avoid: thinking this reverses numbers in place
numbers = [1, 2, 3]
numbers[::-1] # makes a reversed copy, then throws it away
# ✅ Good: save the reversed copy
numbers = [1, 2, 3]
reversed_numbers = numbers[::-1]
print(reversed_numbers) # [3, 2, 1]

One more thing. Going out of range is fine. Slicing is forgiving. If you ask for names[:100] on a short list, Python doesn’t crash. It just gives you everything it has.

✅ Best Practices

Use the shortcuts. Write [:3] instead of [0:3], and [3:] instead of [3:len(names)]. It reads cleaner and clearer.

Reach for [-2:] and [-3:] to grab the last few. It works no matter how long the list is, so you never count by hand.

Give sliced results a clear name like first_three or last_two. The next person reading your code, often you, will thank you.

Remember [::-1] for a quick reverse. But save it to a variable if you actually need it later.

🧩 What You’ve Learned

A quick recap of what you can now do with a list:

  • ✅ Take a part of a list with list[start:stop:step], just like slicing a string.
  • ✅ Remember that stop is not included, so [0:3] gives you three items, at index 0, 1, and 2.
  • ✅ Grab the first few with [:3] and the last few with [-2:].
  • ✅ Take every second item with [::2] and reverse the whole list with [::-1].
  • ✅ Replace a whole chunk in place using slice assignment, like names[1:3] = ["Ben", "Noah"].
  • ✅ Know that reading a slice gives you a new list and leaves the original untouched.

Check Your Knowledge

Test what you learned. Pick an answer for each question, then click Check.

  1. 1

    What does numbers[1:4] return for numbers = [10, 20, 30, 40, 50]?

    Why: The slice starts at index 1 and stops before index 4, so you get the items at 1, 2, and 3: 20, 30, and 40.

  2. 2

    Which slice reverses a list called items?

    Why: A step of -1 walks the list from back to front, so [::-1] returns the whole list reversed.

  3. 3

    What does data[::2] do for data = [1, 2, 3, 4, 5, 6]?

    Why: With start and stop empty and a step of 2, Python walks the whole list two at a time, giving the items at index 0, 2, and 4.

  4. 4

    After running names[1:3] = ['Ben', 'Noah'] on names = ['Alex', 'Riya', 'Arjun', 'Sara'], what is names?

    Why: The slice [1:3] points at 'Riya' and 'Arjun', and slice assignment replaces those two items with 'Ben' and 'Noah'.

🚀 What’s Next?

You can now pull any part of a list apart with slicing. Next you’ll learn a clean, short way to build new lists from old ones, often in a single line.

List Comprehensions

Share & Connect