Archive

Posts Tagged ‘reverse’

Reversing Python list/string objects

May 4th, 2009 No comments

I was interested in the relative performance of reversing a python list using the idom list[::-1] rather than the reverse method of the list class. Additionally I wanted to see if this idom was efficient for reversing strings compared to converting to a list, reversing that list, and joining the elements again using ''.join(reversed_list). Here are the results:

Reverse list, slicing: (0.58000000000000007, 0.64144396781921387)
Reverse list, method: (0.20000000000000018, 0.19551301002502441)
Reverse string, slicing: (0.45000000000000001, 0.49545693397521973)
Reverse string, list: (3.71, 3.7981550693511963)

Read more…