I think it's O(1) but some sources online say O(n)
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
You can also look at the implementation in the CPython source if you want to.
according to http://wiki.python.org/moin/TimeComplexity
it is O(k) where k is the slice size
Time complexity of python slice operation - 💡-string-window - Coding Blocks Discussion Forum
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.
Time and Space complexity of Python list-slicing inside recursive calls
list - Python Set Slice Complexity - Stack Overflow
Videos
What is meant by the assumption that "list-slicing takes constant-time"?
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.
Lists are just a set of references to items they hold. Much like a phone book, a contacts list or an ingredient list for a recipe. If you duplicate or slice the list in Python, you're just creating a new copy of the set of references, the list items themselves aren't handled. This means that the amount of work needed to perform the slice is that little (as it's just administrative), it will take practically the same amount of very little time, regardless of the list and slice sizes.
LeetCode 344
Consider the Python code attached that reverses a string recursively.
My thinking is that the list-slicing takes O(n-1) for both time and space in each of the O(n) recursive calls. Hence, both time and space complexities are O(n2) (the quadratic space complexity dominates the linear recursive space needed).
Is this correct?
O(n+k) is the average case, which includes having to grow or shrink the list to adjust for the number of elements inserted to replace the original slice.
Your case, where you replace the slice with an equal number of new elements, the implementation only takes O(k) steps. But given all possible combinations of number of elements inserted and deleted, the average case has to move the n remaining elements in the list up or down.
See the list_ass_slice function for the exact implementation.
You're right, if you want to know the exact details it's best to use the source. The CPython implementation of setting a slice is in listobject.c.
If I read it correctly, it will...
- Count how many new elements you're inserting (or deleting!)
- Shift the n existing elements of the list over enough places to make room for the new elements, taking O(n) time in the worst case (when every element of the list has to be shifted).
- Copy over the new elements into the space that was just created, taking O(k) time.
That adds up to O(n+k).
Of course, your case is probably not that worst case: you're changing the last k elements of the list, so there might be no need for shifting at all, reducing the complexity to O(k) you expected. However, that is not true in general.