From aae0290ab2691a96f2d461683df6b920ea723904 Mon Sep 17 00:00:00 2001 From: Yiru Chen Date: Wed, 15 Oct 2025 09:04:21 -0400 Subject: [PATCH 1/2] docs(python): add deque rotate() term entry --- .../concepts/deque/terms/rotate/rotate.md | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 content/python/concepts/deque/terms/rotate/rotate.md diff --git a/content/python/concepts/deque/terms/rotate/rotate.md b/content/python/concepts/deque/terms/rotate/rotate.md new file mode 100644 index 00000000000..5bfd6480851 --- /dev/null +++ b/content/python/concepts/deque/terms/rotate/rotate.md @@ -0,0 +1,48 @@ +--- +Title: Python deque rotate() +Description: The rotate() method of deque rotates the elements of the deque by a specified number of steps to the right (positive) or to the left (negative). +Subjects: + - 'Computer Science' + - 'Data Structures' +Tags: + - 'Python' + - 'Deque' + - 'rotate' +CatalogContent: + - 'learn-python-3' + - 'data-structures' +--- + +## rotate() + +The `rotate()` method of Python’s `collections.deque` rotates the elements in the deque by the given number of steps. + +If the number is **positive**, elements are moved from right to left (the rightmost elements move to the beginning). +If the number is **negative**, elements are moved from left to right (the leftmost elements move to the end). + +--- + +## Syntax + +```python +from collections import deque + +deque.rotate(n) + +from collections import deque + +numbers = deque([1, 2, 3, 4, 5]) +numbers.rotate(2) +print(numbers) + +deque([4, 5, 1, 2, 3]) + +from collections import deque + +letters = deque(["A", "B", "C", "D", "E"]) +letters.rotate(-1) +print(letters) + +deque(['B', 'C', 'D', 'E', 'A']) + +``` From 0c79abeecb0b2e194a2de92c5fd1afc6e1476440 Mon Sep 17 00:00:00 2001 From: Mamta Wardhani Date: Mon, 20 Oct 2025 15:38:59 +0530 Subject: [PATCH 2/2] Revise rotate() method documentation in rotate.md Updated the title and description formatting, added syntax and examples for the rotate() method in Python's deque. --- .../concepts/deque/terms/rotate/rotate.md | 65 +++++++++++++++---- 1 file changed, 51 insertions(+), 14 deletions(-) diff --git a/content/python/concepts/deque/terms/rotate/rotate.md b/content/python/concepts/deque/terms/rotate/rotate.md index 5bfd6480851..21960b4fd78 100644 --- a/content/python/concepts/deque/terms/rotate/rotate.md +++ b/content/python/concepts/deque/terms/rotate/rotate.md @@ -1,48 +1,85 @@ --- -Title: Python deque rotate() -Description: The rotate() method of deque rotates the elements of the deque by a specified number of steps to the right (positive) or to the left (negative). +Title: 'rotate()' +Description: 'Rotates the elements of the deque by a specified number of steps to the right (positive) or to the left (negative).' Subjects: - 'Computer Science' - 'Data Structures' Tags: - - 'Python' - 'Deque' - - 'rotate' + - 'Python' CatalogContent: - 'learn-python-3' - - 'data-structures' + - 'paths/computer-science' --- -## rotate() +The **`rotate()`** method of Python’s `collections.deque` rotates the elements in the deque by the specified number of steps. If the number is positive, elements move from right to left (the rightmost elements move to the beginning). If the number is negative, elements move from left to right (the leftmost elements move to the end). -The `rotate()` method of Python’s `collections.deque` rotates the elements in the deque by the given number of steps. +## Syntax -If the number is **positive**, elements are moved from right to left (the rightmost elements move to the beginning). -If the number is **negative**, elements are moved from left to right (the leftmost elements move to the end). +```pseudo +deque.rotate(n) +``` ---- +**Parameters:** -## Syntax +- `n`: The number of steps to rotate the deque. Positive values rotate to the right, negative values rotate to the left. -```python -from collections import deque +**Return value:** -deque.rotate(n) +This method rotates the deque in place and returns `None`. The original deque is modified directly. + +## Example 1: Rotating Numbers to the Right +This example demonstrates rotating a deque of numbers two steps to the right: + +```py from collections import deque numbers = deque([1, 2, 3, 4, 5]) numbers.rotate(2) print(numbers) +``` + +The output of this code is: +```shell deque([4, 5, 1, 2, 3]) +``` + +## Example 2: Rotating Letters to the Left +This example shows rotating a deque of letters one step to the left: + +```py from collections import deque letters = deque(["A", "B", "C", "D", "E"]) letters.rotate(-1) print(letters) +``` +The output of the code is: + +```shell deque(['B', 'C', 'D', 'E', 'A']) +``` + +## Codebyte Example: Mixed Rotation Operations + +The following example illustrates rotating multiple deques in both directions in a single program: + +```codebyte/python +from collections import deque + +numbers = deque([10, 20, 30, 40]) +letters = deque(["X", "Y", "Z"]) + +# Rotate numbers right by 1 +numbers.rotate(1) + +# Rotate letters left by 2 +letters.rotate(-2) +print("Numbers rotated right:", numbers) +print("Letters rotated left:", letters) ```