From f30b50b54a1d8eea1af3748252c226a41dabe807 Mon Sep 17 00:00:00 2001 From: HortusDev Date: Wed, 15 Oct 2025 09:24:16 +0300 Subject: [PATCH 1/2] Add python reverse() method doc Add python reverse() method doc --- .../concepts/deque/terms/reverse/reverse.md | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 content/python/concepts/deque/terms/reverse/reverse.md diff --git a/content/python/concepts/deque/terms/reverse/reverse.md b/content/python/concepts/deque/terms/reverse/reverse.md new file mode 100644 index 00000000000..15f17edc786 --- /dev/null +++ b/content/python/concepts/deque/terms/reverse/reverse.md @@ -0,0 +1,51 @@ +--- +Title: '.reverse()' +Description: 'Reverses the elements of a collections.deque in-place.' +Subjects: + - 'Computer Science' + - 'Data Science' +Tags: + - 'Deques' + - 'Collections' + - 'Methods' + - 'Functions' +CatalogContent: + - 'learn-python-3' + - 'paths/computer-science' +--- + +The `.reverse()` method of a Python `collections.deque` (https://www.codecademy.com/resources/docs/python/collections-module/deque) reverses the order of elements in the deque in-place. It does not accept any arguments and returns `None`. + +## Syntax + +```py +deque.reverse() +``` +## Example + +```python +from collections import deque + +# Create a deque +d = deque([1, 2, 3, 4]) +# Reverse the order of deque's elements +d.reverse() +print(d) # Output: deque([4, 3, 2, 1]) +``` + +## Codebyte Example + +The example below reverses the deque in-place to flip the sentence, then shows the result as a string + +```codebyte/python +from collections import deque + +words = deque(["requests", "pull", "and", "calm", "keep"]) +print("Original deque:", list(words)) +print("Original sentence:", " ".join(words)) +# Reverse the deque in-place to flip the sentence +words.reverse() + +print("After .reverse():", list(words)) +print("Reversed sentence:", " ".join(words)) +``` From dda2c29d59078821312e22e45f3880b5ad2f8c00 Mon Sep 17 00:00:00 2001 From: Mamta Wardhani Date: Thu, 16 Oct 2025 15:53:40 +0530 Subject: [PATCH 2/2] minor updates --- .../concepts/deque/terms/reverse/reverse.md | 35 ++++++++++++++----- 1 file changed, 27 insertions(+), 8 deletions(-) diff --git a/content/python/concepts/deque/terms/reverse/reverse.md b/content/python/concepts/deque/terms/reverse/reverse.md index 15f17edc786..ed31511fa64 100644 --- a/content/python/concepts/deque/terms/reverse/reverse.md +++ b/content/python/concepts/deque/terms/reverse/reverse.md @@ -1,41 +1,59 @@ --- Title: '.reverse()' -Description: 'Reverses the elements of a collections.deque in-place.' +Description: 'Reverses the elements of a deque in-place.' Subjects: - 'Computer Science' - 'Data Science' Tags: - - 'Deques' - 'Collections' - - 'Methods' + - 'Deques' - 'Functions' + - 'Methods' CatalogContent: - 'learn-python-3' - 'paths/computer-science' --- -The `.reverse()` method of a Python `collections.deque` (https://www.codecademy.com/resources/docs/python/collections-module/deque) reverses the order of elements in the deque in-place. It does not accept any arguments and returns `None`. +The **`.reverse()`** method of a Python [`collections.deque`](https://www.codecademy.com/resources/docs/python/collections-module/deque) reverses the order of elements in the `deque` in-place. ## Syntax -```py +```pseudo deque.reverse() ``` + +**Parameters:** + +The `.reverse()` method does not take any parameters. + +**Return value:** + +`None`: The deque is modified in-place. + ## Example -```python +In this example, the elements of a deque are reversed in-place, changing the order of items: + +```py from collections import deque # Create a deque d = deque([1, 2, 3, 4]) + # Reverse the order of deque's elements d.reverse() -print(d) # Output: deque([4, 3, 2, 1]) +print(d) +``` + +The output of this code is: + +```shell +deque([4, 3, 2, 1]) ``` ## Codebyte Example -The example below reverses the deque in-place to flip the sentence, then shows the result as a string +The following example reverses the deque in-place to flip the order of words in a sentence: ```codebyte/python from collections import deque @@ -43,6 +61,7 @@ from collections import deque words = deque(["requests", "pull", "and", "calm", "keep"]) print("Original deque:", list(words)) print("Original sentence:", " ".join(words)) + # Reverse the deque in-place to flip the sentence words.reverse()