Python .reverse()

Anonymous contributor's avatar
Anonymous contributor
Published Oct 29, 2025
Contribute to Docs

The .reverse() method of a Python collections.deque reverses the order of elements in the deque in-place.

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.
    • With Certificate
    • Beginner Friendly.
      24 hours

Syntax

deque.reverse()

Parameters:

The .reverse() method doesn’t take any parameters.

Return value:

The deque is modified in-place.

Example

In this example, the elements of a deque are reversed in-place, changing the order of items:

from collections import deque
# Create a deque
d = deque([1, 2, 3, 4])
# Reverse the order of deque's elements
d.reverse()
print(d)

The output of this code is:

deque([4, 3, 2, 1])

Codebyte Example

The following example reverses the deque in-place to flip the order of words in a sentence:

Code
Output
Loading...

All contributors

Contribute to Docs

Learn Python on Codecademy

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.
    • With Certificate
    • Beginner Friendly.
      24 hours