|
| 1 | +--- |
| 2 | +Title: 'rotate()' |
| 3 | +Description: 'Rotates the elements of the deque by a specified number of steps to the right (positive) or to the left (negative).' |
| 4 | +Subjects: |
| 5 | + - 'Computer Science' |
| 6 | + - 'Data Structures' |
| 7 | +Tags: |
| 8 | + - 'Collections' |
| 9 | + - 'Deque' |
| 10 | + - 'Python' |
| 11 | +CatalogContent: |
| 12 | + - 'learn-python-3' |
| 13 | + - 'paths/computer-science' |
| 14 | +--- |
| 15 | + |
| 16 | +The **`rotate()`** method of Python’s [`collections.deque`](https://www.codecademy.com/resources/docs/python/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). |
| 17 | + |
| 18 | +## Syntax |
| 19 | + |
| 20 | +```pseudo |
| 21 | +deque.rotate(n) |
| 22 | +``` |
| 23 | + |
| 24 | +**Parameters:** |
| 25 | + |
| 26 | +- `n`: The number of steps to rotate the deque. Positive values rotate to the right, negative values rotate to the left. |
| 27 | + |
| 28 | +**Return value:** |
| 29 | + |
| 30 | +This method rotates the deque in place and returns `None`. The original deque is modified directly. |
| 31 | + |
| 32 | +## Example 1: Rotating Days of the Week to the Right |
| 33 | + |
| 34 | +This example demonstrates rotating a deque of weekdays two steps to the right: |
| 35 | + |
| 36 | +```py |
| 37 | +from collections import deque |
| 38 | + |
| 39 | +days = deque(["Mon", "Tue", "Wed", "Thu", "Fri"]) |
| 40 | +days.rotate(2) |
| 41 | +print(days) |
| 42 | +``` |
| 43 | + |
| 44 | +The output of this code is: |
| 45 | + |
| 46 | +```shell |
| 47 | +deque(['Thu', 'Fri', 'Mon', 'Tue', 'Wed']) |
| 48 | +``` |
| 49 | + |
| 50 | +## Example 2: Rotating Movie Titles to the Left |
| 51 | + |
| 52 | +This example showcases rotating a deque of movie titles one step to the left: |
| 53 | + |
| 54 | +```py |
| 55 | +from collections import deque |
| 56 | + |
| 57 | +movies = deque(["Inception", "Avatar", "Interstellar", "Tenet"]) |
| 58 | +movies.rotate(-1) |
| 59 | +print(movies) |
| 60 | +``` |
| 61 | + |
| 62 | +The output of the code is: |
| 63 | + |
| 64 | +```shell |
| 65 | +deque(['Avatar', 'Interstellar', 'Tenet', 'Inception']) |
| 66 | +``` |
| 67 | + |
| 68 | +## Codebyte Example: Opposite Rotations |
| 69 | + |
| 70 | +The following example illustrates rotating two deques in opposite directions within one program: |
| 71 | + |
| 72 | +```codebyte/python |
| 73 | +from collections import deque |
| 74 | +
|
| 75 | +colors = deque(["red", "green", "blue", "yellow"]) |
| 76 | +numbers = deque([100, 200, 300, 400, 500]) |
| 77 | +
|
| 78 | +# Rotate colors right by 1 |
| 79 | +colors.rotate(1) |
| 80 | +
|
| 81 | +# Rotate numbers left by 2 |
| 82 | +numbers.rotate(-2) |
| 83 | +
|
| 84 | +print("Colors rotated right:", colors) |
| 85 | +print("Numbers rotated left:", numbers) |
| 86 | +``` |
0 commit comments