liveBook · Manning

Reversing

As explained in The Quick Python Book, Fourth Edition

The reverse method is a Python list operation that reverses the order of elements in the list in place.

The reverse method reverses the order of a list in place, changing the existing list rather than producing a separately described result. section 5.3

Use the method when the list itself should run from last element to first:

  • Call it with list.reverse(). section 5.3
  • It is a specialized list-modification operation, unlike slice assignment and other general editing techniques. section 5.3
  • Do not confuse reverse() with reversed(): reversed returns an iterator and can traverse selected items without creating a new list. section 7.8.2

Reverse a list in place

x = [1, 3, 5, 6, 7] x.reverse() x [7, 6, 5, 3, 1]

How The Quick Python Book, Fourth Edition covers it

How deep it goes
Defines the specialized in-place operation and demonstrates its output, without discussing implementation internals.
Examples
Demonstrates reversing [1, 3, 5, 6, 7] to [7, 6, 5, 3, 1].
What you can do after
Apply the reverse method to change an existing list from forward order to backward order.
How it connects
Connects list reversal with list modification, slicing, sorting, and the distinct reversed function.

Also explained in

Compare how 3 books explain reversing →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of The Quick Python Book, Fourth Edition. The section links point to the parts of the book it draws on.