What's "better" the reverse method or the reversed built-in function?

rectangletangle picture rectangletangle · Jul 25, 2011 · Viewed 19.1k times · Source

What is typically regarded as more Pythonic/better/faster to use, the reverse method or the reversed built-in function?

Both in action:

_list = list(xrange(4))

print _list

rlist = list(reversed(_list))

print rlist

_list.reverse()

print _list

Answer

kindall picture kindall · Jul 25, 2011

foo.reverse() actually reverses the elements in the container. reversed() doesn't actually reverse anything, it merely returns an object that can be used to iterate over the container's elements in reverse order. If that's what you need, it's often faster than actually reversing the elements.