Yes, you are right, it is O(n) where n - length of list.
Look here for more information: https://www.ics.uci.edu/~pattis/ICS-33/lectures/complexitypython.txt
Time complexity of reversed() in Python 3 - Stack Overflow
What is the time complexity of reversed() in Python 3? I think the answer would be O(1) but I want to clarify whether it is right or wrong. More on stackoverflow.com
stackoverflow.com
python - Does looping through a reversed(list) add to the time complexity of my function? - Stack Overflow
Short but simple question. I have been studying time complexity for a coding interview and I can not find a concise answer to this. I am aware that this question exists on SO. What is the time complexity of Python List Reverse?. More on stackoverflow.com
stackoverflow.com
Time complexity of Python a[::-1]
A previously present answer of O(n) is the only one here, and while I agree that it is a sane answer, I've done some benchmarking in iPython and it seems that a[::-1] is significantly slower than using the reversed() builtin: In [14]: a = list(range(0, 1000000)) In [15]: %timeit a[::-1] 4.85 ms ± 41.4 µs per loop (mean ± std. dev. of 7 runs, 100 loops each) In [16]: %timeit reversed(a) 70.6 ns ± 0.592 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each) This leads me to believe that using the stepped slice operator isn't "smart" enough to use a typical reversing algorithm. Tested with Python v3.8.5 and iPython v7.13.0 Edit: A big thank you to all the comments below showing the flaw in my calculations. Reversed does indeed return an iterator. For those not versed in Python, this means that the reversed result is not an in-memory list, but is instead a code-only representation of the list that can be interacted with as if the list existed (to a degree, there are things iterators cannot do). Since Python doesn't need to read list a to create the iterator, we've failed to test the run time of creating a reversed list using this method. I have repeated my benchmarks but instead forced a list to be constructed of the reversed iterator. Following on from u/spyr03 's comment, I have also used three sizes of list and included the creation of a copy without reversing in order to make a deduction on the complexity used in each method. The results: In [1]: a = list(range(0, 1_000_000)) In [2]: b = list(range(0, 10_000_000)) In [3]: c = list(range(0, 100_000_000)) In [4]: %timeit list(a) 4.82 ms ± 82.1 µs per loop (mean ± std. dev. of 7 runs, 100 loops each) In [5]: %timeit a[::-1] 4.53 ms ± 44.8 µs per loop (mean ± std. dev. of 7 runs, 100 loops each) In [7]: %timeit list(reversed(a)) 6.68 ms ± 166 µs per loop (mean ± std. dev. of 7 runs, 100 loops each) In [8]: %timeit list(b) 58.1 ms ± 165 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) In [9]: %timeit b[::-1] 57.4 ms ± 146 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) In [10]: %timeit list(reversed(b)) 82.6 ms ± 681 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) In [11]: %timeit list(c) 723 ms ± 32.7 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) In [12]: %timeit c[::-1] 749 ms ± 11.6 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) In [13]: %timeit list(reversed(c)) 958 ms ± 36.1 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) Conclusions that can be made: reversed() is consistently slower than a straight copy and a reversed copy using slicing. My suspicion is that this is caused by overhead of the iterator. A straight copy and a sliced reverse copy take approximately the same amount of time. Increasing the list size by 10 increases the run-time of each method by approximately 10. We can therefore deduce that all three methods have a runtime of O(n), with the reversed() operator having a larger constant factor than the other methods. More on reddit.com
r/algorithms
14
19
November 29, 2020
Why is list.reverse() an O(n) operation?
Well, because it's an in-place operation on a list (not an array). Indexing tricks work great up until the next append operation or similar. There's the built-in reversed which gives you a reverse-order iterator (and as such is O(1), naturally). More on reddit.com
February 3, 2025 - To help you choose the best approach, let's compare the methods we discussed and analyze their complexities. Let’s discuss what this table indicates in reference to the worst and best methods for Python list reversal. ... reverse() Method: This is the most efficient in terms of time and space if you need to modify the list in place.
November 7, 2025 - This means they all require a linear amount of time relative to the list size. This refers to the additional memory used by the algorithm. reverse() Method: The space complexity is O(1), which is optimal for memory usage.
Python · Open in Mimo · Open in Mimo · Copy Code · letters = ['a', 'b', 'c'] print("Before:", letters) letters.reverse() print("After:", letters) # Output: ['c', 'b', 'a'] The reverse() method is fast and memory efficient since it doesn’t create a new list. Time Complexity: O(n) Space ...
March 21, 2020 - ... Here’s your free PDF cheat sheet showing you all Python list methods on one simple page. Click the image to download the high-resolution PDF file, print it, and post it to your office wall: ... The time complexity of the reverse() operation is O(n) for a list with n elements.
April 24, 2022 - N.B. Time complexity is how many times we have traversed through for loop to reverse the number which is near to n/2(n being the no of elements of array) so linear time complexity .. Space complexity is measured as you may have noticed in this example we did not use any temporary data structure to reverse the existing array list, we just used the same one so the program did not use any extra memory for that.
January 21, 2026 - Time Complexity: O(n) Space Complexity: reverse() → O(1) Slicing / Loop → O(n) Expecting reverse() to return a value · Confusing reverse() with sorted() Forgetting that slicing creates a new list · Featurereverse()reversed()Modifies original listYesNoReturn typeNoneIteratorMemory efficientYesYes · How do you reverse a list in Python?
June 19, 2024 - The format [a : b : c] in slicing states that from an inclusive to b exclusive, count in increments of c. In the above code, a and b are blank and c is -1. So it iterates the entire list counting from the last element to the first element resulting in a reversed list. ... # Python code to reverse a list using slicing # Original list original_list = [110, 220, 330, 440, 550] print('Original list:', original_list) # Reversing the list using slicing reversed_list = original_list[::-1] # Printing the reversed list elements print('Reversed list elements:') for element in reversed_list: print(element)
October 28, 2024 - It modifies the original list directly: numbers = [1, 2, 3, 4, 5] numbers.reverse() print(numbers) # Output: [5, 4, 3, 2, 1] What’s happening here? The `reverse()` method: - Modifies the list in place (no new list is created) - Doesn’t return a new value (returns None) - Uses constant extra space O(1) - Has linear time complexity O(n) ...
May 7, 2024 - Time Complexity: O · ( l · o · g · 2 · ( n · ) ) O(log2(n))O(log2(n)) Space Complexity: O · ( 1 · ) O(1)O(1) The reversed() function returns an iterator that accesses the given sequence in the reverse order.
April 28, 2023 - ['p', 'r', 'e', 'p', 'b', 'y', 't', 'e', 's'] Using reverse() ['s', 'e', 't', 'y', 'b', 'p', 'e', 'r', 'p'] Time Complexity: O(N) will be the time complexity to reverse a list in Python using the reverse method.
June 5, 2024 - The reverse() method has a time complexity of O(n), where n is the number of elements in the list. ... Previous: Python List remove() Method.
March 15, 2023 - The logic to solve linked list reverse in Python using the tail recursive method remains the same, however, a new tail recursive function is added. The function reverseLinkedListTailRecursive reverses the linked list by reversing the links one by one from the beginning until the last node is reached. When the last node is reached, it makes this node the new head of the list. We iterate over the linked list once using recursion, hence the time complexity is
February 15, 2025 - Rather than use a loop + append to then reverse the text, the list.reverse() method is used to perform an in-place reversal. join() is then used to turn the list into a string. This takes O(n) time complexity because list.reverse() & join() ...
March 20, 2025 -O(n^2) (Quadratic Time): An operation with O(n^2) time complexity takes time proportional to the square of the input size. As the input size grows, the running time increases much more rapidly. This is a slicing operation in Python. Slicing creates a new list with the elements in reverse order.
May 18, 2020 - The order of elements in the original list is changed. Scenarios where order of elements in the original list can be altered and keeping a low memory footprint is desired . Python lists can be reversed using the [::-1] slicing suffix.