This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author Yury
Recipients Yury
Date 2020-03-27.12:35:37
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <>
Hi all,

In Python, I need a tool to reverse part of a list (tail) quickly.
I expected that


would do it inplace with the performance similar to nums.reverse().
However, it doesn't work at all. The fastest way to reverse a part of
the list that I found is like this:

nums[start:end] = nums[end:start-1:-1]

But it is 30 times slower than pure reverse(). The patch below adds
a region support for the reverse(). It works as fast as I expect.

The test results and script are like this:

            nums.reverse() 0.006764888763427734
            nums = nums[::-1] 0.10066413879394531
            nums.reverse(-L/2) 0.003548145294189453
            nums.reverse(L/2, L) 0.003538370132446289
            nums = nums[:L/2] + nums[L:L/2-1:-1] 0.19934582710266113
            nums[L/2:L] = nums[L:L/2-1:-1] 0.11419057846069336

import time

nums = list(range(10000000))
L = len(nums)
LL = int(L/2)

t = time.time()
print('nums.reverse()\t\t\t\t', str(time.time() - t))

t = time.time()
nums = nums[::-1]
print('nums = nums[::-1]\t\t\t', str(time.time() - t))

t = time.time()
print('nums.reverse(-L/2)\t\t\t', time.time() - t)

t = time.time()
nums.reverse(LL, L)
print('nums.reverse(L/2, L)\t\t\t', time.time() - t)

t = time.time()
nums = nums[:LL] + nums[L : LL - 1 : -1]
print('nums = nums[:L/2] + nums[L:L/2-1:-1]\t', time.time() - t)

t = time.time()
nums[LL:L] = nums[L:LL-1:-1]
print('nums[L/2:L] = nums[L:L/2-1:-1]\t\t', time.time() - t)

If there is better way to reverse lists, can someone point me at the right direction? If not, I'll be happy to fix all existing issues and upstream
this approach.


Date User Action Args
2020-03-27 12:35:37Yurysetrecipients: + Yury
2020-03-27 12:35:37Yurysetmessageid: <>
2020-03-27 12:35:37Yurylinkissue40088 messages
2020-03-27 12:35:37Yurycreate