List slicing with the start greater than the large

How exactly does it work when you have the start of a slice index, greater than the end.
Is this some weird bug, or intended behavior ? I couldn’t find anything online related to the documentation of this.

x = [1,2,3,4,5]
x[3:1] = [’ ']
print(x) # [1, 2, 3, ‘’, 4, 5]

first time posting here, thanks

…huh. I did not expect that to work, haven’t seen that one before!

I guess technically this is documented here, in the second case. You are assigning the contents of the list [''] to the slice x[3:1] which is an empty slice right after the 3rd element of the list x. It’s not very intuitive that this would work because x[3:1] seems like nonsense but it’s the same as x[3:3]–an empty slice, so assigning to it is like saying “insert the following elements into the list at this location”

Hi,

it appears that any number less than the first is ignored where only the first number is valid. I am surprised that an exception was not raised. By the way, you do not need brackets on the term on the right side of the equation:

x[3:1] = ' '  # This would suffice - (only value is needed - not the brackets)

Try these:

x = [1, 2, 3, 4, 5, 6, 7, 8]

x[3:5] = '  '
>>> [1, 2, 3, ' ', 6, 7, 8]  # result

x = [1, 2, 3, 4, 5, 6, 7, 8]  # reset
x[3:6] = '  '
>>> [1, 2, 3, ' ', 7, 8]  # result

Work out a few more test case scenarios to get more familiar with list slicing.

By the way, you can also check your results with the following:

list(enumerate(x))  # this will give you the index, and the value in that index of the final result as a sanity check if so desired

Yesn’t: a one-length string happens to be an iterable that returns itself, but all other values will not work the same way as when they are in a list.

1 Like

Omitting the brackets is subtly different in a tricky way: it relies on the fact that strings are themselves iterable. If you try x[3:3] = ' ' * 2 (with two spaces) you see that it iterates over the right-hand side and the result is [1, 2, 3, ' ', ' ', 4, 5] (two elements, one space each). Whereas x[3:3] = [' ' * 2] gives you [1, 2, 3, ' ', 4, 5] (one element of two spaces).

edit: apparently discourse refuses to display two consecutive spaces, even in a code block. That’s annoying.

So they aren’t equivalent here, and in particular omitting the brackets won’t insert anything if the string is empty, as in the OP (I think, but they didn’t use code formatting so I’m not sure)

Did you try those? :wink:

2 Likes

Ahh, yes, you are correct. This is Python’s version of a slight of hand. Very subtle.

I did. I copied and pasted the results to reply. :grimacing:

Yes, I have been corrected.

Hm, I got different results:

x = [1, 2, 3, 4, 5, 6, 7, 8]

x[3:5] = '  '
# x is now [1, 2, 3, ' ', ' ', 6, 7, 8] (i.e. two elements)

x = [1, 2, 3, 4, 5, 6, 7, 8]  # reset
x[3:6] = '  '
# x is now [1, 2, 3, ' ', ' ', 7, 8] (again two elements, but we removed three)

There is a single space between the apostrophes in the test code that I pasted, not two.

I’ll take your word for it, either my browser is doing something weird or Discourse is, but these examples aren’t displaying correctly for me

I just took a screenshot of the IDLE shell interpreter. How do you post a png file?
Ok, here it is. The x[3:5] appears to only have one index with the empty string. However, it has
truncated indexes 3 and 4 (but not two empty strings).

codeSnapshot

It’s fine, I wouldn’t worry about it. Just formatting weirdness.

Sorry for the late response, I was out doing some things.

I didn’t even realize that slice insertions were iterated over, so that’s something new for me lol. Good to know.

I’ll probably never use this. I just saw this slice in a book and decided to explore and learn more about it. I wish things like this were documented more but it’s fine.
Thanks for the help!

>>> x = list(range(8))
>>> x
[0, 1, 2, 3, 4, 5, 6, 7]
>>> x[3:1]
[]
>>> x[3:1:-1]
[3, 2]
>>> x[3:1:-1] = 'ab'
>>> x
[0, 1, 'b', 'a', 4, 5, 6, 7]

Isn’t it lovely!