惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

J
Java Code Geeks
月光博客
月光博客
D
DataBreaches.Net
云风的 BLOG
云风的 BLOG
F
Fortinet All Blogs
T
The Blog of Author Tim Ferriss
Stack Overflow Blog
Stack Overflow Blog
Blog — PlanetScale
Blog — PlanetScale
aimingoo的专栏
aimingoo的专栏
U
Unit 42
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
MyScale Blog
MyScale Blog
T
Tailwind CSS Blog
N
Netflix TechBlog - Medium
B
Blog
博客园_首页
G
Google Developers Blog
Recent Announcements
Recent Announcements
博客园 - 【当耐特】
P
Proofpoint News Feed
博客园 - 司徒正美
Hugging Face - Blog
Hugging Face - Blog
MongoDB | Blog
MongoDB | Blog
Last Week in AI
Last Week in AI

Stonecharioteer on Tech

I Traced My Traffic Through a Home Tailscale Exit Node What Was I Reading Last? In Three Not-So-Easy Pieces Dogfooding Is Hard Code blocks in your books, finally GoForGo v0.9.0 Merrilin - We built an app to read books I use a Macbook now Data Structures & Algorithms - Preparing for Interviews Using a local DNS namespace for local service discovery Direction KOllector - Publishing KOReader Highlights gbt: branches touched in the last 24 hours A Soiree into Symbols in Ruby Some Smalltalk about Ruby Loops Ruby Blocks Returning from Ruby Blocks, Procs and Lambdas My Linux Laptop Finally Works: How Claude Helped Me Fix Years of Annoyances TIL: Watchexec - Modern File Watching for Development Workflows A Less Busy Mind GoForGo - Learn Go through live examples Migrating My Old Blog to Hugo with Claude The Qtile Window Manager: A Python-Powered Tiling Experience Read the RFCs that Built the Internet Py-x-Protobuf - Or How I Learned to Stop Worrying and Love Protocol Buffers New Beginnings Leaving ChainSafe Systems Screen Lock for Cinnamon Desktop using Zenity and Terminal Commands Crews Not Teams A System for Getting Better at LeetCode So Far So Rust
Python Reverse a List
2025-04-13 · via Stonecharioteer on Tech

There are a couple of ways to reverse a list in python:

1
2
3
4
5
my_list = [1,2,3,"apples","banana","fish","chicken"]
reverse_list_one = my_list[::-1]
reverse_list_one = reversed(my_list)
reverse_list_zero = my_list
reverse_list_zero.reverse()

Of these three ways, I prefer using reversed because it is readable and there’s no ambiguity about whether it does it in place or not. It should be obvious to most people that .reverse() reverses a list in place, but I prefer the verbosity of the method that returns a reversed list. It’s a stylistic choice.

The [::-1] syntax though is unreadable unless you know where it comes from. The syntax is List[start:stop_at:step_count]. This can be used to collect items of a list in a specific way. You could use it to collect all the alternate items in a list through my_list[::2], or every third item from the first to the seventh index, through my_list[0:7:2] (If you don’t want to include the last item, remember that python’s list intervals are half-open).

So [::-1] means collect all the items in this array but move through it -1 items at a time, meaning, start with the last item first and move backwards.

But which one of these is faster? We can use the timeit module to test this out.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
import timeit
import random

my_list = [random.randrange(10**6) for _ in range(10**3)]

trial_1 = timeit.timeit("reversed(my_list)", number=10**4, globals=dict(my_list=my_list))
print(trial_1)
my_list_1 = my_list
trial_2 = timeit.timeit("my_list_1.reverse()", number=10**4, globals=dict(my_list_1=my_list_1))
print(trial_2)
trial_3 = timeit.timeit("my_list[::-1]", number=10**4, globals=dict(my_list=my_list))
print(trial_3)

This attempts to reverse a list of a thousand numbers ranging from 0 to a million ten thousand times. On my X13 Flow laptop and with python 3.12.3 this gets me the following output:

0.0005502169997271267
0.0013474229999701492
0.012336962999597745

I’m fairly surprised myself that reversed is the fastest way to reverse a list in Python. I half expected to tell you that it’d be the list notation syntax. And it’s faster than the in-place method, which isn’t surprising since it’s not trying to move things around to save memory. This StackOverflow answer shows that it’s creating an iterator and not returning a list like the list notation syntax would, so using list(reversed(my_list)) would be a fairer comparison.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
import timeit
import random

my_list = [random.randrange(10**6) for _ in range(10**3)]

trial_1 = timeit.timeit("reversed(my_list)", number=10**4, globals=dict(my_list=my_list))
my_list_1 = my_list
trial_2 = timeit.timeit("my_list_1.reverse()", number=10**4, globals=dict(my_list_1=my_list_1))
trial_3 = timeit.timeit("my_list[::-1]", number=10**4, globals=dict(my_list=my_list))
trial_4 = timeit.timeit("list(reversed(my_list))", number=10**4, globals=dict(my_list=my_list))
print(f"""
{ trial_1= }
{ trial_2= }
{ trial_3= }
{ trial_4= }
""")

Now we get a more realistic solution.

 trial_1= 0.0005320420000316517
 trial_2= 0.0013524949999919045
 trial_3= 0.011964209999860032
 trial_4= 0.032413619000180915

Honestly though, this doesn’t prove anything. If I needed a reversed list to iterate over, the iterator approach with reversed is the best way to do it. If I need an in-memory version of it, the list notation syntax is clean. In-place reversal is useful only if I’m completely sure I would like to reverse the list in place. This is useful when the list is perhaps an attribute of a class, for example.