-1

What are the differences between those four ways of reversing a list? Which one is faster?

1.

list.reverse()

2.

list(reversed(list))

3.

list[::-1]

4.

while(start < end):
    list[start], list[end] = list[end], list[start]
    start += 1
    end -= 1`
anonym
  • 17
  • 1
  • 4

2 Answers2

0

I'll rename the list variable to xs to avoid confusion with the function list.

xs.reverse() mutates the original list, so that the values to the reference xs changes everywhere.

reversed(xs) creates an iterator, so calling list on it makes a new list with the values reversed. No mutations happen here.

xs[::-1] creates a new list reversed. This has the same effect as list(reversed(xs)). Not sure about which one is more performant though.

The procedure you wrote is also mutating, so that the original list xs changes.

If performance is not an issue, I would recommend non-mutating versions, as it can lead to surprising behaviour. In this case xs[::-1] is the simplest.

anthony-khong
  • 141
  • 2
  • 4
-1
list.reverse()

Works on a reference. It means, that whenever you use it, you change the original list permanently.

list[::-1]

Is called reversing by slicing. It is not changing the state of the current list permanently. It is quite ineffective, since it creates a copy of the list that you are using it on, which takes up memory.

reversed()

Is not really that different. It works like slicing, but people often use it in for loop to create a reverse iterator, so a new list won't be created. If you print out a reversed list with reversed(), you can do it, but you will basically make a copy of a existing list, which is quite ineffective - but if you want to, you can do something like:

print(list(reversed(list_name)))

or if you want to print out elements of this reversed list

print(*reversed(list_name))

And I think the last solution works exactly like the first one, but it takes a lot of code. You have included only a part of it, but the full code would look something like that:

    listt = [1,2,3,4,5,6,7]
start = 0
end = len(listt)-1

while(start < end):
    listt[start], listt[end] = listt[end], listt[start]
    start += 1
    end -= 1

print(listt)

And that's a lot more code. Also, I am not pretty sure if it won't actually be slower.

It really depends on what you want in your program. If you simply want to show how the reversed list looks like, use slicing or reversed().

You can of course use them like the first method - reverse(), but you will have to assign those lists to a new variable, since (for example slicing) is not updating current list as mentioned above.

For example:

listt = [1,2,3,4,5,6,7]
a = listt[::-1]
print(listt)
#[1, 2, 3, 4, 5, 6, 7]
print(a)
#[7, 6, 5, 4, 3, 2, 1]
todovvox
  • 170
  • 10
  • Oh, sorry, I've meant print(list(reversed(list_name))) since it prints out reversed list, but print(*reversed(list)) is okay as you wrote too, since it prints out values of the reversed list. I've adapted my words poorly here, true. [::-1] has it's own usage. I don't know why I've wrote "quite ineffective". Maybe it was somehow in context to the previous sentence, but I've changed it and left it as it was. – todovvox Oct 31 '20 at 04:54