O(n) where n is the length of the slice. Slicing is just "copy part of the list" so time complexity is the same as copy. Answer from novel_yet_trivial on reddit.com
Discussions

python - Big-O of list slicing - Stack Overflow
Getting a slice is O(i_2 - i_1). This is because Python's internal representation of a list is an array, so you can start at i_1 and iterate to i_2. For more information, see the Python Time Complexity wiki entry More on stackoverflow.com
🌐 stackoverflow.com
algorithm - Python slice complexity? - Stack Overflow
What is the time complexity of this short program? I'm thinking it's linear, but then again, in the if statement there's a Python slice. I'm wondering how it would contribute to the complexity: def More on stackoverflow.com
🌐 stackoverflow.com
python - Time complexity of string slice - Stack Overflow
What's the time complexity of slicing a Python string? Given that Python strings are immutable, I can imagine slicing them being either O(1) or O(n) depending upon how slicing is implemented. I ne... More on stackoverflow.com
🌐 stackoverflow.com
List-Slicing

It means the time it takes for something to happen doesn't depend on the size of the input. It deals with the subject of Time Complexity. As far as Python is concerned, list-slicing doesn't take constant time, it's bounded by a some kind of factor depending on the slice operation. You can refer to this page which documents the time complexity for common operations.

Anything with O(1) is something that's completed in constant time. The time complexity for get, set, and del slice operations are affected by either n, k, or some combination of those two. The page itself explains what those variables mean more concisely than I can, so refer to the introduction section for more information on those.

More on reddit.com
🌐 r/learnpython
6
7
October 3, 2017
People also ask

What is string slicing in Python?
String slicing in Python refers to the process of extracting a portion (substring) of a string by specifying the start and end indices. It allows you to work with substrings within a larger string, making it easier to manipulate and analyze text data.
🌐
upgrad.com
upgrad.com › home › tutorials › software & tech › string slicing in python
String Slicing in Python
Are there any performance considerations when using string slicing?
String slicing in Python is efficient and typically has a time complexity of O(k), where k is the length of the slice. However, creating many slices of a large string can increase memory usage. If performance is a concern, consider using other data structures or techniques for text processing.
🌐
upgrad.com
upgrad.com › home › tutorials › software & tech › string slicing in python
String Slicing in Python
What is the difference between string slicing and string indexing?
String slicing extracts a range of characters from a string, returning a new string. String indexing, on the other hand, extracts a single character from a string at a specific position, returning a character (string of length 1).
🌐
upgrad.com
upgrad.com › home › tutorials › software & tech › string slicing in python
String Slicing in Python
🌐
Coding Blocks
discuss.codingblocks.com › t › time-complexity-of-python-slice-operation › 99497
Time complexity of python slice operation - 💡-string-window - Coding Blocks Discussion Forum
August 1, 2020 - what is the time complexity of slicing in python. Example: lis[2:] I wrote a program in leetcode with lis slicing and without lis slicing and still got the same time result.
🌐
FavTutor
favtutor.com › blogs › python-list-slicing
Python List Slicing (with Examples)
September 23, 2024 - List slicing in Python has a time complexity of O(k), where k is the number of elements in the slice.
🌐
Upgrad
upgrad.com › home › tutorials › software & tech › string slicing in python
String Slicing in Python
October 22, 2024 - String indexing, on the other hand, extracts a single character from a string at a specific position, returning a character (string of length 1). String slicing in Python is efficient and typically has a time complexity of O(k), where k is the ...
Find elsewhere
🌐
Stack Overflow
stackoverflow.com › questions › 62138656 › python-slice-complexity
algorithm - Python slice complexity? - Stack Overflow
If abs(m - n) is bounded by a constant, the loop only executes a constant number of times. ... Python's time complexity is O(k) where k is the length of the slice, so your function is O(n * k), which is not linear, yet still not exponential.
🌐
CodeRivers
coderivers.org › blog › list-slicing-time-complexity-python
Understanding List Slicing Time Complexity in Python - CodeRivers
February 22, 2026 - The basic syntax is list[start... between each element in the slice. The time complexity of basic list slicing list[start:stop] (when step is 1) is O(k), where k is the number of elements in the resulting slice....
🌐
Medium
medium.com › @ivanmarkeyev › understanding-python-list-operations-a-big-o-complexity-guide-49be9c00afb4
Understanding Python List Operations: A Big O Complexity Guide | by Ivan Markeev | Medium
June 4, 2023 - When inserting or deleting an element at the beginning or middle of a Python list, the remaining elements must be shifted to accommodate the change. As a result, these operations have a linear time complexity of O(n).
🌐
Medium
andrewwhit.medium.com › time-complexity-of-string-slicing-in-python-db25177d0c48
Time Complexity of String Slicing in Python | by Andrew | Medium
February 22, 2026 - String slicing in Python is a powerful and concise operation, but its time complexity is O(k), where k is the length of the resulting substring. This is because strings are immutable, and slicing creates a new string by copying k characters ...
Top answer
1 of 3
58

Short answer: str slices, in general, copy. That means that your function that does a slice for each of your string's n suffixes is doing O(n2) work. That said, you can avoid copies if you can work with bytes-like objects using memoryviews to get zero-copy views of the original bytes data. See How you can do zero copy slicing below for how to make it work.

Long answer: (C)Python str do not slice by referencing a view of a subset of the data. There are exactly three modes of operation for str slicing:

  1. Complete slice, e.g. mystr[:]: Returns a reference to the exact same str (not just shared data, the same actual object, mystr is mystr[:] since str is immutable so there is no risk to doing so)
  2. The zero length slice and (implementation dependent) cached length 1 slices; the empty string is a singleton (mystr[1:1] is mystr[2:2] is ''), and low ordinal strings of length one are cached singletons as well (on CPython 3.5.0, it looks like all characters representable in latin-1, that is Unicode ordinals in range(256), are cached)
  3. All other slices: The sliced str is copied at creation time, and thereafter unrelated to the original str

The reason why #3 is the general rule is to avoid issues with large str being kept in memory by a view of a small portion of it. If you had a 1GB file, read it in and sliced it like so (yes, it's wasteful when you can seek, this is for illustration):

with open(myfile) as f:
    data = f.read()[-1024:]

then you'd have 1 GB of data being held in memory to support a view that shows the final 1 KB, a serious waste. Since slices are usually smallish, it's almost always faster to copy on slice instead of create views. It also means str can be simpler; it needs to know its size, but it need not track an offset into the data as well.

How you can do zero copy slicing

There are ways to perform view based slicing in Python, and in Python 2, it will work on str (because str is bytes-like in Python 2, supporting the buffer protocol). With Py2 str and Py3 bytes (as well as many other data types like bytearray, array.array, numpy arrays, mmap.mmaps, etc.), you can create a memoryview that is a zero copy view of the original object, and can be sliced without copying data. So if you can use (or encode) to Py2 str/Py3 bytes, and your function can work with arbitrary bytes-like objects, then you could do:

def do_something_on_all_suffixes(big_string):
    # In Py3, may need to encode as latin-1 or the like
    remaining_suffix = memoryview(big_string)
    # Rather than explicit loop, just replace view with one shorter view
    # on each loop
    while remaining_suffix:  # Stop when we've sliced to empty view
        some_constant_time_operation(remaining_suffix)
        remaining_suffix = remaining_suffix[1:]

The slices of memoryviews do make new view objects (they're just ultra-lightweight with fixed size unrelated to the amount of data they view), just not any data, so some_constant_time_operation can store a copy if needed and it won't be changed when we slice it down later. Should you need a proper copy as Py2 str/Py3 bytes, you can call .tobytes() to get the raw bytes obj, or (in Py3 only it appears), decode it directly to a str that copies from the buffer, e.g. str(remaining_suffix[10:20], 'latin-1').

2 of 3
4

It all depends on how big your slices are. I threw together the following two benchmarks. The first slices the entire string and the second only a little bit. Curve fitting with this tool gives

# s[1:-1]
y = 0.09 x^2 + 10.66 x - 3.25

# s[1:1000]
y = -0.15 x + 17.13706461

The first looks quite linear for slices of strings up to 4MB. I guess this really measures the time taken to construct a second string. The second is pretty constant, although it's so fast it's probably not that stable.

import time

def go(n):
    start = time.time()
    s = "abcd" * n
    for j in xrange(50000):

        #benchmark one
        a = s[1:-1]

        #benchmark two
        a = s[1:1000]

    end = time.time()
    return (end - start) * 1000

for n in range(1000, 100000, 5000):
    print n/1000.0, go(n)
🌐
YouTube
youtube.com › codetube
python list slicing time complexity - YouTube
Download this code from https://codegive.com List slicing is a powerful feature in Python that allows you to extract portions of a list. Understanding the ti...
Published   December 19, 2023
Views   6
🌐
CSDN
devpress.csdn.net › python › 6304528ec67703293080b167.html
Time complexity of string slice - Python - DevPress - CSDN
August 23, 2022 - ... will its time complexity be O(n) or O(n2), where n is len(big_string)? Short answer: str slices, in general, copy. That means that your function that does a slice for each of your string's n suffixes is doing O(n2) work. That said, you can avoid copies if you can work with bytes-like objects ...
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-alternate-range-slicing-in-list
Python | Alternate range slicing in list - GeeksforGeeks
April 18, 2023 - The original list : [2, 4, 6, 8, 9, 10, 12, 16, 18, 20, 7, 30] The alternate range sliced list : [8, 9, 10, 20, 7, 30] Time complexity: O(n), where n is the length of the list, as the list comprehension and the enumerate() function both have ...
🌐
Ancisoft
ancisoft.com › blog › time-complexity-of-string-slice
What's the Time Complexity of Python String Slicing? Implications for Iterating Over Suffixes of Large Strings — ancisoft.com
To grasp why string slicing has a specific time complexity, we first need to understand a core property of Python strings: immutability.
🌐
LeetCode
leetcode.com › problems › rotate-array › discuss › 1379911 › python-slicing-one-line-on-time-on-space
Slicing | One Line | O(N) time | O(N) space - Rotate Array
August 3, 2021 - Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.