This thread talks about slicing syntax and the full slice [:] for copying.

Do you know about slicing in Python 🐍?

It's that thing that looks like indexing but has colons : in it.

For example, my_list[2:6] is list slicing!

When you do list slicing, did you know [:] gets the whole thing?

But what's the point in that..?

I'll explain 🚀

>>> my_str = "Hello, world!"
>>> my_str[:5]
>>> my_str[7:]
>>> my_str[:]
'Hello, world!'

First, how does slicing work?

The idea is simple:

When you write a slice, Python copies the sliced chunk and gives it to you.

The key here is to realise the slice is a copy of the sliced portion.

Notice, below, how the slice is independent of the original list:

>>> l = list(range(10))
>>> l
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> sub = l[2:6]
>>> sub[0] = 42
>>> l
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> sub
[42, 3, 4, 5]

When you slice, you can omit the first number in the slice.

That means “slice from the beginning”.

Likewise, omitting the second number in the slice means “slice until the end”.

So, if you omit both..?

“Slice from the beginning until the end.”

>>> l = [42, 73, 0, 10]
>>> l[:3]
[42, 73, 0]
>>> l[1:]
[73, 0, 10]
>>> l[:]
[42, 73, 0, 10]

So, what's the point of using [:]?

We use it to get a copy of the original thing!

That is when using [:] is useful:

When you want a copy so as to not change the original.

For example, you can use [:] on a list if you are going to modify it and want to keep the original:

>>> words = "How is your day going?".split()
>>> words
['How', 'is', 'your', 'day', 'going?']
>>> their_words = words[:]
>>> their_words[2] = "their"
>>> their_words
['How', 'is', 'their', 'day', 'going?']
# changed     ^^^^^^^
>>> words
['How', 'is', 'your', 'day', 'going?']
# original    ^^^^^^

Did this make any sense?

If you want to learn more about slicing, you can always check this Pydon't:

To check if you understood, I have a challenge for you 💪

Why is it ALWAYS 100% unnecessary to use [:] on a string?

my_str = "Hello, world!"
#     vvv never use this with strings.

This article was generated automatically from this thread I published on Twitter @mathsppblog.

I hope you learned something new! If you did, consider following the footsteps of the readers who bought me a slice of pizza 🍕. Your small contribution helps me produce this content for free and without spamming you with annoying ads.

Previous Post Next Post

Blog Comments powered by Disqus.