Python - reversed() VS [::-1] , Which one is faster?
Last Updated :
19 Jun, 2024
Python lists can be reversed using many Python method such as using slicing method or using reversed() function. This article discusses how both of these work and Which one of them seems to be the faster one and Why.
Reversing a list using Slicing
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
# 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)
Output:
Original list : [110, 220, 330, 440, 550]
Reversed list elements :
550
440
330
220
110
Reversing a list Using reversed() built-in function
The built-in reversed() function in Python returns an iterator object rather than an entire list.
Python
# Python code to reverse
# a list using reversed()
ls = [110, 220, 330, 440, 550]
print('Original list :', ls)
# list reverse
ls = reversed(ls)
print('Iterator object :', ls)
print('Reversed list elements :')
for element in ls:
print(element)
Output:
Original list : [110, 220, 330, 440, 550]
Iterator object : <list_reverseiterator object at 0x7fbd84e0b630>
Reversed list elements :
550
440
330
220
110
Time Complexity : O(n), where n is the size of the given array.
Auxiliary Space : O(n)
Conclusion
Both slicing (A[::-1]
) and the reversed()
method provide a time complexity of O(n), where n is the length of the string. This means they take the same amount of time to reverse a string, regardless of its length.
However, in practice, A[::-1]
may be slightly faster than reversed()
for large input sizes. This is because reversed()
creates an iterator object, which can be marginally slower than directly accessing the characters in the string. Additionally, A[::-1]
has a slightly lower space complexity, making it a more efficient choice for reversing strings with large input sizes.
Moreover, the A[::-1]
function is easier to read and understand, which can be beneficial in many applications. Ultimately, the best choice of function will depend on the specific needs of the application, but A[::-1]
is generally a faster and more straightforward option for reversing strings, especially with large inputs.
Similar Reads
How to reverse column order in a matrix with Python? In this article, we will see how to reverse the column order of a matrix in Python. Examples: Input: arr = [[10,20,30], [40,50,60], [70,80,90]] Output: 30 20 10 60 50 40 90 80 70 Input: arr = [[15,30], [45,60], [75,90], [105,120]] Output: 30 15 60 45 90 75 120 105 Matrices are created in python by u
2 min read
Reverse Words in a Given String in Python In this article, we explore various ways to reverse the words in a string using Python. From simple built-in methods to advanced techniques like recursion and stacks. We are going to see various techniques to reverse a string.Using split() and join()Using split() and join() is the most common method
2 min read
How to reverse a String in Python Reversing a string is a common task in Python, which can be done by several methods. In this article, we discuss different approaches to reversing a string. One of the simplest and most efficient ways is by using slicing. Letâs see how it works:Using string slicingThis slicing method is one of the s
4 min read
Python | Reverse a numpy array As we know Numpy is a general-purpose array-processing package that provides a high-performance multidimensional array object, and tools for working with these arrays. Let's discuss how can we reverse a Numpy array. Using flip() function to Reverse a Numpy array The numpy.flip() function reverses t
2 min read
Python List Reverse() The reverse() method is an inbuilt method in Python that reverses the order of elements in a list. This method modifies the original list and does not return a new list, which makes it an efficient way to perform the reversal without unnecessary memory uses.Let's see an example to reverse a list usi
2 min read
Reversing a List in Python In this article, we are going to explore multiple ways to reverse a list. Python provides several methods to reverse a list using built-in functions and manual approaches. The simplest way to reverse a list is by using the reverse() method. Let's take an example to reverse a list using reverse() met
4 min read