Python – reversed() VS [::-1] , Which one is faster?

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.

Code: Reversing a list using Slicing.

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python code to reverse
# a list using slicing
  
ls = [110, 220, 330
      440, 550]
print('Original list :', ls)
  
# list reverse
ls = ls[::-1]
  
print('Reversed list elements :')
for element in ls:
  print(element)

chevron_right


Output:

Original list : [110, 220, 330, 440, 550]
Reversed list elements :
550
440
330
220
110

Explanation : The format[a : b : c] in slicing states that from an inclusive to b exclusive, count in increments of c. In above code, a and b is 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.

Code: Reversing a list Using reversed() built-in function.



Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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)

chevron_right


Output: 

Original list : [110, 220, 330, 440, 550]
Iterator object : <list_reverseiterator object at 0x7fbd84e0b630>
Reversed list elements :
550
440
330
220
110

Explanation : The built-in reversed() function in Python returns an iterator object rather than an entire list.

Conclusion : 

For a comparatively large list, under time constraints, it seems that the reversed() function performs faster than the slicing method. This is because reversed() just returns an iterator that iterates the original list in reverse order, without copying anything whereas slicing creates an entirely new list, copying every element from the original list. For a list with 106 Values, the reversed() performs almost 20,000 better than the slicing method. If there is a need to store the reverse copy of data then slicing can be used but if one only wants to iterate the list in reverse manner, reversed() is definitely the better option.

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.