Search code examples
pythonpython-3.xsequenceslicerange-checking

Why does Python allow out-of-range slice indexes for sequences?


So I just came across what seems to me like a strange Python feature and wanted some clarification about it.

The following array manipulation somewhat makes sense:

p = [1,2,3]
p[3:] = [4] 
p = [1,2,3,4]

I imagine it is actually just appending this value to the end, correct?
Why can I do this, however?

p[20:22] = [5,6]
p = [1,2,3,4,5,6]

And even more so this:

p[20:100] = [7,8]
p = [1,2,3,4,5,6,7,8]

This just seems like wrong logic. It seems like this should throw an error!

Any explanation?
-Is it just a weird thing Python does?
-Is there a purpose to it?
-Or am I thinking about this the wrong way?


Solution

  • Part of question regarding out-of-range indices

    Slice logic automatically clips the indices to the length of the sequence.

    Allowing slice indices to extend past end points was done for convenience. It would be a pain to have to range check every expression and then adjust the limits manually, so Python does it for you.

    Consider the use case of wanting to display no more than the first 50 characters of a text message.

    The easy way (what Python does now):

    preview = msg[:50]
    

    Or the hard way (do the limit checks yourself):

    n = len(msg)
    preview = msg[:50] if n > 50 else msg
    

    Manually implementing that logic for adjustment of end points would be easy to forget, would be easy to get wrong (updating the 50 in two places), would be wordy, and would be slow. Python moves that logic to its internals where it is succint, automatic, fast, and correct. This is one of the reasons I love Python :-)

    Part of question regarding assignments length mismatch from input length

    The OP also wanted to know the rationale for allowing assignments such as p[20:100] = [7,8] where the assignment target has a different length (80) than the replacement data length (2).

    It's easiest to see the motivation by an analogy with strings. Consider, "five little monkeys".replace("little", "humongous"). Note that the target "little" has only six letters and "humongous" has nine. We can do the same with lists:

    >>> s = list("five little monkeys")
    >>> i = s.index('l')
    >>> n = len('little')
    >>> s[i : i+n ] = list("humongous")
    >>> ''.join(s)
    'five humongous monkeys'
    

    This all comes down to convenience.

    Prior to the introduction of the copy() and clear() methods, these used to be popular idioms:

    s[:] = []           # clear a list
    t = u[:]            # copy a list
    

    Even now, we use this to update lists when filtering:

    s[:] = [x for x in s if not math.isnan(x)]   # filter-out NaN values
    

    Hope these practical examples give a good perspective on why slicing works as it does.